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。我怎樣才能解決這個問題?
你能發佈你的API響應嗎? – Sonali8890
@ Sonali8890你可以去http://services.groupkt.com/country/get/all看到迴應 – manman