[英]How to get JSON ARRAY and JSON object response from retrofit response?
I have worked with file upload using retrofit. 我曾使用改造进行文件上传。 It works fine. 工作正常。 But how do handle the retrofit success response. 但是如何处理改造成功的响应。 and How do i create serialization model class for below Json array and Json object. 以及如何为下面的Json数组和Json对象创建序列化模型类。
{
"result": [{
"fileId": 869,
"status": 1,
"pcData": {
"id": 652,
"filename": "IMG_20161122_175344.jpg",
"filepath": "uploads\/peoplecaddie\/files\/1743_1481109145_IMG_20161122_175344.jpg"
}
}]
}
Here is My call method 这是我的通话方法
Call<ServerResponse> call = service.upload("817b6ce98fd759e7f148b948246df6c1", map, idReq, fileCountReq, fileTypeReq, platformReq, externalIDReq);
call.enqueue(new Callback<ServerResponse>() {
@Override
public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
ServerResponse serverResponse = response.body();
Log.e("serverResponse", "serverResponse" + serverResponse.toString());
if (serverResponse != null) {
}
}
@Override
public void onFailure(Call<ServerResponse> call, Throwable t) {
}
});
I am having doubt that how to implement ServerResponse model class with JSON array field, JSON object field and string values which is inside JSON object. 我怀疑如何使用JSON对象中的JSON数组字段,JSON对象字段和字符串值来实现ServerResponse模型类。
public class ServerResponse {
//How to handle my response with in this model class.
}
Please help me to solve this. 请帮我解决这个问题。 Thanks in advance. 提前致谢。
Create Classes like below and use ServerResponse
as the model class while call
, 如下创建类,并在call
将ServerResponse
用作模型类,
ServerResponse.class ServerResponse.class
public class ServerResponse {
@SerializedName("result")
private ArrayList<Result> mResult;
public ArrayList<Result> getResult() {
return mResult;
}
public void setResult(ArrayList<Result> result) {
mResult = result;
}
}
Result.class 结果类
public class Result {
@SerializedName("fileId")
private int mFileId;
@SerializedName("status")
private String mstatus;
@SerializedName("pcData")
private PcData mPcData;
public int getFileId() {
return mFileId;
}
public void setFileId(int fileId) {
mFileId = fileId;
}
public String getMstatus() {
return mstatus;
}
public void setMstatus(String mstatus) {
this.mstatus = mstatus;
}
public PcData getPcData() {
return mPcData;
}
public void setPcData(PcData pcData) {
mPcData = pcData;
}
}
PcData.class PcData.class
private class PcData {
@SerializedName("id")
private int mId;
@SerializedName("filename")
private String mFileName;
@SerializedName("filepath")
private String mFilePath;
public int getId() {
return mId;
}
public void setId(int id) {
mId = id;
}
public String getFileName() {
return mFileName;
}
public void setFileName(String fileName) {
mFileName = fileName;
}
public String getFilePath() {
return mFilePath;
}
public void setFilePath(String filePath) {
mFilePath = filePath;
}
}
And your call should be like this: 您的呼叫应如下所示:
Call<ServerResponse> call = service.upload("817b6ce98fd759e7f148b948246df6c1", map, idReq, fileCountReq, fileTypeReq, platformReq, externalIDReq);
call.enqueue(new Callback<ServerResponse>() {
@Override
public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
ServerResponse serverResponse = response.body();
if (serverResponse != null) {
//below is how you can get the list of result
List<Result> resultList = response.getResult();
}
}
@Override
public void onFailure(Call<ServerResponse> call, Throwable t) {
}
});
You can declare your service using below code 您可以使用以下代码声明您的服务
Call<ServerResponse<ArrayList<YourModel>>>
and 和
public class ServerResponse<T> {
private T result;
public T getResult() {
return data;
}
public void setResult(T data) {
this.data = data;
}
}
then you will receive ServerResponse
in onResponse
Method and get lsit by calling method getResult of SErverResponse. 那么您将在onResponse
方法中收到ServerResponse
并通过调用SErverResponse的getResult方法获得lsit。
You can use RxJava with Retrofit 您可以将RxJava与Retrofit一起使用
First create Pojo for your JSON Response, you can use http://www.jsonschema2pojo.org/ for doing this. 首先为您的JSON响应创建Pojo,您可以使用http://www.jsonschema2pojo.org/进行此操作。
Then Create a Singleton class for Retrofit 然后创建一个Singleton类进行改造
public class RetroSingleton {
private static RetroSingleton ourInstance = new RetroSingleton();
public static RetroSingleton getInstance() {
return ourInstance;
}
private RetroSingleton() {
}
public RestApi getRestApi() {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(logging);
OkHttpClient client = httpClient.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ConstUrl.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(client)
.build();
return retrofit.create(RestApi.class);
}
}
Then Create a Interface where you can define all your methods for doing network calls 然后创建一个接口,您可以在其中定义用于进行网络调用的所有方法
public interface RestApi {
/*
*@POST or @GET type of call
*/
@POST("url")// either full url or path of url
Observable<YourResponsePojo> methodName(@Query("nameOfField") String field);
@GET("url")
Observable<YourResponsePojo> methodName();
}
Finally for using the call you can use 最后,使用通话您可以使用
RetroSingleton.getInstance().getRestApi().methodName()
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<YourResponsePojo>() {
@Override
public void onCompleted() {
hideProgressDialog();
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(YourResponsePojo yourResponsePojo) {
// yourResponsePojo contains all of your response body
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.