如何使用来自Retrofit 2.0的gson 2.0 on - onResponse

时间:2016-05-08 09:34:37

标签: android json gson retrofit

我正在使用新的改装2.0进行http呼叫, 并得到一个回调, 我想使用gson 2.0库来解析json obj并且能够做到

jsonData.sector[2].sectorOne

这是我的回调:

retrofitApi.Factory.getInstance().getCallData().enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

                try {

                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.d("myLogs", "failed to Retrive Data");
                Log.d("myLogs", "becouse: "+t);
                largeTextVar.setText("failed: " + t);
            }
        });

这就是我的回调看起来像

{
  "data": {
    "sector": [
      [
        {
          "scanned": false,
          "sectorOne": "",
          "sectorTwo": "",
          "sectorThree": ""
        },
        {
          "scanned": false,
          "sectorOne": "",
          "sectorTwo": "",
          "sectorThree": ""
        }
      ]
    ]
  },
  "curserLocation": {
    "elevation": 30,
    "horizontal": 105
  }
}

我正在使用:

compile 'com.squareup.retrofit2:retrofit:2.0.1'
compile 'com.squareup.retrofit2:converter-gson:2.0.1' 

我到处看看如何做到这一点,但我无法找到一个简单的解决方案, 实现这一目标的最简单,最简单的方法是什么?

1 个答案:

答案 0 :(得分:0)

Okey我将解释如何将JSON响应转换为POJO

首先,您必须在JSON Schema 2 POJO

中创建一个POJO类
  1. 粘贴您的示例JSON回复
  2. 选择来源类型:JSON
  3. 注释风格:Gson
  4. 它将为您生成POJO课程。

    然后在您的onResponse方法中:

            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if(response.code()==200){
                Gson gson = new GsonBuilder().create();
                    YourPOJOClass yourpojo=new YourPOJOClass ();
                    try {
                        yourpojo= gson.fromJson(response.body().toString(),YourPOJOClass.class); 
                    } catch (IOException e) {
                        // handle failure to read error
                        Log.v("gson error","error when gson process");  
                    }
            }
    

    别忘了添加compile 'com.google.code.gson:gson:2.4'

      

    另一种方法:创建一个类似于上面的pojo类。

    在您的API中:

    @POST("endpoint")
    public Call<YourPOJOClass> exampleRequest();
    

    调用时:

        OkHttpClient okClient = new OkHttpClient.Builder().connectTimeout(10, TimeUnit.SECONDS).build();
    
        Gson gson = new GsonBuilder()
                .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
                .create();
    
        Retrofit client = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .client(okClient)
                .build();
    
        YourApiClass service = client.create(YourApiClass.class);
    
        Call<YourPOJOClass> call=service.exampleRequest();
        call.enqueue(new Callback<YourPOJOClass>() {
            @Override
            public void onResponse(Call<YourPOJOClass> call, Response<YourPOJOClass> response) {
                //already Gson convertor factory converted your response body to pojo
                response.body().getCurserLocation();
            }
    
            @Override
            public void onFailure(Call<YourPOJOClass> call, Throwable t) {
    
            }
        });