我目前正在研究改造2并遇到了一个问题。
我正在使用免费的api
$(function() {
var pressed, pressX, pressY,
dragged,
offset = 3; // helps detect when the user really meant to drag
$(document)
.on('mousedown', '.thing', function(e) {
pressX = e.pageX;
pressY = e.pageY;
pressed = true;
})
.on('mousemove', '.thing', function(e) {
if (!pressed) return;
dragged = Math.abs(e.pageX - pressX) > offset ||
Math.abs(e.pageY - pressY) > offset;
})
.on('mouseup', function() {
dragged && console.log('Thing dragged');
pressed = dragged = false;
});
});
以下是我的POJO
RestResponse
http://services.groupkt.com/country/get/all
结果
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;
}
}
ApiInterface
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;
}
}
MainActivity
public interface ApiInterface {
@GET("country/get/all")
Call<RestResponse> getCountry();
}
改造调用onResponse回调,但是当我试图从response.body()获取结果时,它返回null。我该如何解决这个问题?
答案 0 :(得分:1)
因此,您需要一个不同的类来保存RestResponse
,因为您获得的Json响应具有第一级对象RestResponse
,然后该类是您拥有的类。 / p>
所以你需要这样的东西:
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());
}
});
试试这个,这应该有效。