如何通过改造响应获得JSON ARRAY和JSON对象响应?

时间:2016-12-07 12:06:00

标签: android serialization file-upload retrofit

我使用改造来处理文件上传。它工作正常。但是如何处理改造成功的反应。以及如何为下面的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"
            }
        }]
    }

这是我的通话方法

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

            }
        });

我怀疑如何使用JSON数组字段,JSON对象字段和JSON对象内的字符串值实现ServerResponse模型类。

public class ServerResponse {
    //How to handle my response with in this model class.

}

请帮我解决这个问题。提前谢谢。

3 个答案:

答案 0 :(得分:5)

创建如下所示的类,并使用ServerResponse作为模型类,call

<强> 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

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

你的电话应该是这样的:

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

        }
    });

答案 1 :(得分:3)

您可以使用以下代码声明您的服务

Call<ServerResponse<ArrayList<YourModel>>>

public class ServerResponse<T> {

private T result;
public T getResult() {
    return data;
}

public void setResult(T data) {
    this.data = data;
}


}

然后您将在ServerResponse方法中收到onResponse并通过调用SErverResponse的getResult方法获取lsit。

答案 2 :(得分:0)

您可以将RxJava与Retrofit一起使用

首先为您的JSON响应创建Pojo,您可以使用http://www.jsonschema2pojo.org/来执行此操作。

然后为Retrofit创建一个Singleton类

<label>

然后创建一个接口,您可以在其中定义进行网络呼叫的所有方法

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

最后,对于使用电话,您可以使用

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

}