2017-03-05 50 views
0

我目前正在研究改裝2並遇到問題。改裝2在其成員變量上返回null

我使用的是免費的API

http://services.groupkt.com/country/get/all 

這裏是我的POJO

RestResponse

public class RestResponse { 

    @SerializedName("messages") 
    @Expose 
    private List<String> messages; 
    @SerializedName("result") 
    @Expose 
    private List<Result> result; 

    public List<String> getMessages() { 
     return messages; 
    } 

    public void setMessages(List<String> messages) { 
     this.messages = messages; 
    } 

    public List<Result> getResult() { 
     return result; 
    } 

    public void setResult(List<Result> result) { 
     this.result = result; 
    } 

} 

結果

public class Result { 

    @SerializedName("name") 
    @Expose 
    private String name; 
    @SerializedName("alpha2_code") 
    @Expose 
    private String alpha2Code; 
    @SerializedName("alpha3_code") 
    @Expose 
    private String alpha3Code; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getAlpha2Code() { 
     return alpha2Code; 
    } 

    public void setAlpha2Code(String alpha2Code) { 
     this.alpha2Code = alpha2Code; 
    } 

    public String getAlpha3Code() { 
     return alpha3Code; 
    } 

    public void setAlpha3Code(String alpha3Code) { 
     this.alpha3Code = alpha3Code; 
    } 
} 

ApiInterface

public interface ApiInterface { 

    @GET("country/get/all") 
    Call<RestResponse> getCountry(); 

} 

MainActivity

ApiInterface apiService = ApiClient.getmRetrofitClient().create(ApiInterface.class); 
     Call<RestResponse> call = apiService.getCountry(); 
     call.enqueue(new Callback<RestResponse>() { 
      @Override 
      public void onResponse(Call<RestResponse> call, Response<RestResponse> response) { 
       List<Result> results = response.body().getResult(); 
      } 

      @Override 
      public void onFailure(Call<RestResponse> call, Throwable t) { 
       Log.e("onFailure", t.getMessage()); 
      } 
     }); 

改裝調用onResponse回調,但是當我試圖從response.body得到的結果()返回null。我怎樣才能解決這個問題?

+0

你能發佈你的API響應嗎? – Sonali8890

+0

@ Sonali8890你可以去http://services.groupkt.com/country/get/all看到迴應 – manman

回答

1

所以你需要一個不同的類來保存RestResponse,因爲你得到的Json響應具有第一級對象RestResponse,然後那個是你擁有的類。

enter image description here

所以,你會需要這樣的東西:

public class CountryResponse { 

    @SerializedName("RestResponse") 
    @Expose 
    private RestResponse restResponse; 

    public RestResponst getRestResponse() { return restResponse; } 
    public void setRestResponse(RestResponse restResp) {restResponse = restResp; } 
} 

而你只需要改變API調用行:

ApiInterface apiService = ApiClient.getmRetrofitClient().create(ApiInterface.class); 
    Call<CountryResponse> call = apiService.getCountry(); 
    call.enqueue(new Callback<CountryResponse>() { 
     @Override 
     public void onResponse(Call<CountryResponse> call, Response<CountryResponse> response) { 
      List<Result> results = response.body().getRestResponse().getResult(); 
     } 

     @Override 
     public void onFailure(Call<CountryResponse> call, Throwable t) { 
      Log.e("onFailure", t.getMessage()); 
     } 
}); 

試試這個,這應該工作。