【问题标题】:Get JSONArray from Retrofit Response从改造响应中获取 JSONArray
【发布时间】:2018-06-11 21:17:44
【问题描述】:

//这是我从服务器获取的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 响应中获取 code、contentType、maxjsonlength、message、status 的值时无法解析 JSONArray。请帮我解决问题。

//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();
        }
    });

【问题讨论】:

  • 使用本网站:jsonschema2pojo.org
  • @NabinBhandari 我用的一样。
  • 请在您的帖子中添加改造 API 代码。
  • 尝试评论 profileimagepath 获取代码然后尝试它,如果它有效,那么这意味着你没有得到它的对象。
  • @Ankita 只是为了测试你可以将 profileimagepath 类型更改为 String 并尝试一次吗?

标签: android arrays json api retrofit


【解决方案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模型基准

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);

最后,如下调用web函数,

    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));
            }
        }
    });

}

【讨论】:

    【解决方案2】:
    @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 解决了我的问题,没有改变代码中的任何其他内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-23
      • 2018-01-29
      • 2017-07-26
      • 2017-04-19
      • 2016-01-06
      • 2017-07-02
      • 1970-01-01
      相关资源
      最近更新 更多