android - 从改造响应中获取 JSONArray

标签 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、maxjsonlength、消息、状态的值时无法解析 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();
        }
    });

最佳答案

始终尝试使用 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;
    }

 }

//模型数据

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

}

关于android - 从改造响应中获取 JSONArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48056364/

相关文章:

java - 如何动态地在整数数组中添加元素?

android - 使用 Android 模拟器更改 TelephonyManager.getNetworkCountryIso 输出

java - 如何在 Android 中获取 LinkedIn 联系人或好友列表?

php - 如何按值对关联数组的数组进行排序

java - Java中long数组转int数组

java - java restful服务中如何消费json参数

c# - 如何访问多个 Xamarin "Picker"内的选定参数

javascript - 在 JavaScript 中,如何检查一个数组是否包含另一个数组中的所有元素(包括计数)?

c# - 键值未知时反序列化 JSON

javascript - 调用nodejs文件的onclick函数