Java 在给定JSON响应的情况下,如何在改造2.0中编写响应类

Java 在给定JSON响应的情况下,如何在改造2.0中编写响应类,java,android,api,retrofit2,Java,Android,Api,Retrofit2,我有一个JSON响应,需要创建一个类来捕获响应字段,以便在我的应用程序中使用它 这是JSON响应: { "coord": { "lon": 24.44, "lat": 60.12 }, "weather": [ { "id": 800, "main": "Clear", "description": "clear sky", "icon": "01d" } ], "base": "station

我有一个JSON响应,需要创建一个类来捕获响应字段,以便在我的应用程序中使用它

这是JSON响应:

 {
  "coord": {
    "lon": 24.44,
    "lat": 60.12
  },
  "weather": [
    {
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "01d"
    }
  ],
  "base": "stations",
  "main": {
    "temp": 290.15,
    "pressure": 1006,
    "humidity": 42,
    "temp_min": 290.15,
    "temp_max": 290.15
  },
  "visibility": 10000,
  "wind": {
    "speed": 7.2,
    "deg": 330
  },
  "clouds": {
    "all": 0
  },
  "dt": 1497432000,
  "sys": {
    "type": 1,
    "id": 5019,
    "message": 0.0028,
    "country": "FI",
    "sunrise": 1497401838,
    "sunset": 1497469706
  },
  "id": 649630,
  "name": "Kirkkonummi",
  "cod": 200
}
这是我为使用改装2.0编写的响应类:

package pojo;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;


public class ResultObject<T> {

    @Expose
    @SerializedName("name")
    public String name;


    @Expose
    @SerializedName("id")
    public int id;

    @Expose
    @SerializedName("cod")
    public int cod;


    @Expose
    @SerializedName("dt")
    public long dt;

    @Expose
    @SerializedName("visibility")
    public int visibility;

    @Expose
    @SerializedName("base")
    public String base;

    @Expose
    @SerializedName("main")
    public T main;

    public T getMain() {
        return main;
    }

    @Expose
    @SerializedName("weather")
    public Weather weather;

    public class Weather{

        @Expose
        @SerializedName("description")
        public String description;

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }
    }

    public Weather getWeather() {
        return weather;
    }

}
package helper;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class RetrofitHelper<T> {

    public T getApi(Class<T> tClass){
        return getApi(tClass, QuickPreferences.BASE_URL);
    }

    public T getApi(Class<T> tClass, String URL){
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

        httpClient.addInterceptor(logging);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(URL)
                .client(httpClient.build())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        return retrofit.create(tClass);
    }
}
这是API调用:

 mApi = new RetrofitHelper<AuthApi>().getApi(AuthApi.class);

        currentCall = mApi.currentData(lat,lon,units,key);

        currentCall.enqueue(new Callback<ResultObject<Main>>() {
            @Override
            public void onResponse(Call<ResultObject<Main>> call,
                                   Response<ResultObject<Main>> response) {

                Log.d("enter","enter");
                //Log.d("temp",new Integer(response.body().getMain().getTemp()).toString());
                mData=response.body();
                temp_current.setText(mData.getMain().getTemp());
                status_current.setText(mData.getWeather().getDescription());


                pd.hide();
            }

            @Override
            public void onFailure(Call<ResultObject<Main>> call, Throwable t) {
                Log.d("enterf","Log.getStackTraceString(new Exception())");
                pd.hide();

            }
        });
这就是我如何使用改装2.0:

package pojo;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;


public class ResultObject<T> {

    @Expose
    @SerializedName("name")
    public String name;


    @Expose
    @SerializedName("id")
    public int id;

    @Expose
    @SerializedName("cod")
    public int cod;


    @Expose
    @SerializedName("dt")
    public long dt;

    @Expose
    @SerializedName("visibility")
    public int visibility;

    @Expose
    @SerializedName("base")
    public String base;

    @Expose
    @SerializedName("main")
    public T main;

    public T getMain() {
        return main;
    }

    @Expose
    @SerializedName("weather")
    public Weather weather;

    public class Weather{

        @Expose
        @SerializedName("description")
        public String description;

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }
    }

    public Weather getWeather() {
        return weather;
    }

}
package helper;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class RetrofitHelper<T> {

    public T getApi(Class<T> tClass){
        return getApi(tClass, QuickPreferences.BASE_URL);
    }

    public T getApi(Class<T> tClass, String URL){
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

        httpClient.addInterceptor(logging);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(URL)
                .client(httpClient.build())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        return retrofit.create(tClass);
    }
}
包助手;
导入okhttp3.OkHttpClient;
导入okhttp3.logging.HttpLoggingInterceptor;
进口改装2.改装;
进口改装2.converter.gson.GsonConverterFactory;
公营班级助理{
公共T getApi(类tClass){
返回getApi(tClass、QuickPreferences.BASE\uURL);
}
公共T getApi(类tClass,字符串URL){
HttpLoggingInterceptor logging=新的HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClient=新建OkHttpClient.Builder();
httpClient.addInterceptor(日志记录);
改装改装=新改装.Builder()
.baseUrl(URL)
.client(httpClient.build())
.addConverterFactory(GsonConverterFactory.create())
.build();
返回改装。创建(tClass);
}
}

使用以下站点,您可以创建响应类:

尝试在那里打印日志跟踪,例如:t.printStackTrace();在您的问题中添加此项您是否向您的客户端添加了Gson转换器?@RakshitNawani我已经使用了此项(Log.getStackTraceString(new Exception()),并按照您所说的进行了更新(do see)@sarthakGandhi我添加了客户端(do see)如果它对您有效,您能将其标记为正确答案吗?