从Retrofit Response获取JSONArray

时间:2018-01-02 06:16:24

标签: android arrays json api retrofit

//这是我从服务器获取的JSON

{
"code": "200",
"contentType": "Json",
"data": [{
    "createdon": "2017-12-2",
    "fname": "abhay",
    "mobileno": "1234567890",
    "userid": 1234,
    "profileimagepath": null
}],
"maxjsonlength": 1233,
"message": "Success",
"status": true
}

在从Retrofit响应中获取代码,contentType,JSONArray,消息,状态的值时无法解析maxjsonlength。请帮我纠正这个问题。

//Model 
public class Example {

@SerializedName("code")
@Expose
private String code;
@SerializedName("contentType")
@Expose
private String contentType;
@SerializedName("data")
@Expose
private List<Datum> data = null;
@SerializedName("maxjsonlength")
@Expose
private Integer maxjsonlength;
@SerializedName("message")
@Expose
private String message;
@SerializedName("status")
@Expose
private Boolean status;

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public String getContentType() {
return contentType;
}

public void setContentType(String contentType) {
this.contentType = contentType;
}

public List<Datum> getData() {
return data;
}

public void setData(List<Datum> data) {
this.data = data;
}

public Integer getMaxjsonlength() {
return maxjsonlength;
}

public void setMaxjsonlength(Integer maxjsonlength) {
this.maxjsonlength = maxjsonlength;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public Boolean getStatus() {
return status;
}

public void setStatus(Boolean status) {
this.status = status;
}

}

// Model of data JsonArray
public class Datum {

@SerializedName("createdon")
@Expose
private String createdon;
@SerializedName("fname")
@Expose
private String fname;
@SerializedName("mobileno")
@Expose
private String mobileno;
@SerializedName("userid")
@Expose
private Integer userid;
@SerializedName("profileimagepath")
@Expose
private Object profileimagepath;

public String getCreatedon() {
return createdon;
}

public void setCreatedon(String createdon) {
this.createdon = createdon;
}

public String getFname() {
return fname;
}

public void setFname(String fname) {
this.fname = fname;
}

public String getMobileno() {
return mobileno;
}

public void setMobileno(String mobileno) {
this.mobileno = mobileno;
}

public Integer getUserid() {
return userid;
}

public void setUserid(Integer userid) {
this.userid = userid;
}

public Object getProfileimagepath() {
return profileimagepath;
}

public void setProfileimagepath(Object profileimagepath) {
this.profileimagepath = profileimagepath;
}

}

//这就是我向服务器发送请求的方式:

 @Headers({
    "Content-Type:application/json",
    "Bearer:mlm_token"
    })

@GET("api/v1.0/****/TestApi/{id}")
Call<Example> getTestJson(@Query("id") int userId);

//这是Retrofit Api代码

 DrawerMethods msgInterface = (DrawerMethods) NetworkModule
            .getInstance().createClassInstance(DrawerMethods.class);

       Call<Example> mLoginCall = msgInterface.getTestJson(userId);
        mLoginCall.enqueue(new Callback<Example>() {
        @Override
        public void onResponse(Call<Example> call, Response<Example> response) {
            if (response.isSuccessful()) {
                if (response.body() != null) {
                    //SomeCode
                    }else
                        Toast.makeText(TodayMessage.this, "Invalid credentials!", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(TodayMessage.this, "Failed to get Response", Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(TodayMessage.this, "Failed to get Response", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFailure(Call<Example> call, Throwable t) {

            Toast.makeText(TodayMessage.this, "Unable to connect to server", Toast.LENGTH_SHORT).show();
        }
    });

2 个答案:

答案 0 :(得分:1)

始终尝试使用this创建JSON POJO。 对于您的项目,请添加以下2个模型类

//获取模型

 public class Get {

    @SerializedName("code")
    @Expose
    private String code;
    @SerializedName("contentType")
    @Expose
    private String contentType;
    @SerializedName("data")
    @Expose
    private List<Datum> data = null;
    @SerializedName("maxjsonlength")
    @Expose
    private Integer maxjsonlength;
    @SerializedName("message")
    @Expose
    private String message;
    @SerializedName("status")
    @Expose
    private Boolean status;

    public String getCode() {
    return code;
    }

    public void setCode(String code) {
    this.code = code;
    }

    public String getContentType() {
    return contentType;
    }

    public void setContentType(String contentType) {
    this.contentType = contentType;
    }

    public List<Datum> getData() {
    return data;
    }

    public void setData(List<Datum> data) {
    this.data = data;
    }

    public Integer getMaxjsonlength() {
    return maxjsonlength;
    }

    public void setMaxjsonlength(Integer maxjsonlength) {
    this.maxjsonlength = maxjsonlength;
    }

    public String getMessage() {
    return message;
    }

    public void setMessage(String message) {
    this.message = message;
    }

    public Boolean getStatus() {
    return status;
    }

    public void setStatus(Boolean status) {
    this.status = status;
    }

 }

// FOR MODEL Datum

public class Datum {

@SerializedName("createdon")
@Expose
private String createdon;
@SerializedName("fname")
@Expose
private String fname;
@SerializedName("mobileno")
@Expose
private String mobileno;
@SerializedName("userid")
@Expose
private Integer userid;
@SerializedName("profileimagepath")
@Expose
private Object profileimagepath;

public String getCreatedon() {
return createdon;
}

public void setCreatedon(String createdon) {
this.createdon = createdon;
}

public String getFname() {
return fname;
}

public void setFname(String fname) {
this.fname = fname;
}

public String getMobileno() {
return mobileno;
}

public void setMobileno(String mobileno) {
this.mobileno = mobileno;
}

public Integer getUserid() {
return userid;
}

public void setUserid(Integer userid) {
this.userid = userid;
}

public Object getProfileimagepath() {
return profileimagepath;
}

public void setProfileimagepath(Object profileimagepath) {
this.profileimagepath = profileimagepath;
}

}

对于API调用,请使用

//LoginPojo login check
 @Headers({
    "Content-Type:application/json",
    "Bearer:mlm_token"
    })

@GET("api/v1.0/****/TestApi/{id}")
Call<Get> savePost(@Query("id") int userId);

最后,调用网络功能如下,

    public void logindataPost() {
    mAPIService.savePost().enqueue(new Callback<Get>() {
        @Override
        public void onResponse(Call<Get> call, Response<Get> response) {
            if (response.isSuccessful()) {
                Log.d("code", "" + response.body().getCode());
                Log.d("contentType", "" + response.body().getContentType());
                Log.d("createdon", "" + response.body().getData().get(0).getCreatedon());
                Log.d("fname", "" + response.body().getData().get(0).getFname());
                Log.d("mobileno", "" + response.body().getData().get(0).getMobileno());
                Log.d("userid", "" + response.body().getData().get(0).getUserid());
                Log.d("profileimagepath", "" + response.body().getData().get(0).getProfileimagepath());
                Log.d("maxjsonlength", "" + response.body().getMaxjsonlength());
                Log.d("message", "" + response.body().getMessage());
                Log.d("status", "" + response.body().getStatus());
            } else {

            }
        }

        @Override
        public void onFailure(Call<Get> call, Throwable tr) {
            if (tr != null) {
                Log.d("TEST  Inside fail if", Log.getStackTraceString(tr));
            }
        }
    });

}

答案 1 :(得分:0)

@Headers({
"Content-Type:application/json",
"Bearer:mlm_token"
})

@GET("api/v1.0/****/TestApi/{id}")
//Call<Get> savePost(@Query("id") int userId)
Call<Get> savePost(@Path("id") int userId);

将@Query替换为@Path解决了我的问题,没有更改代码中的任何其他内容。