在没有库的情况下,我曾尝试过这样做,并且工作正常。 但是,使用改装库,我得到它返回此日志:
致命的例外:主要 过程:com.example.lenovo.retrof,PID:21218 java.lang.NullPointerException:尝试调用虚方法 ' java.util.List中 com.example.lenovo.retrof.POJO.MainResponse.getResults()'在null 对象参考 com.example.lenovo.retrof.MainActivity $ 1.onResponse(MainActivity.java:53) 在 retrofit2.ExecutorCallAdapterFactory $ ExecutorCallbackCall $ 1 $ 1.run(ExecutorCallAdapterFactory.java:68) 在android.os.Handler.handleCallback(Handler.java:815) 在android.os.Handler.dispatchMessage(Handler.java:104) 在android.os.Looper.loop(Looper.java:207) 在android.app.ActivityThread.main(ActivityThread.java:5728) at java.lang.reflect.Method.invoke(Native Method) 在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:789) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
这是我崩溃时的代码:
gitrep.enqueue(new Callback<MainResponse>() {
@Override
public void onResponse(Call<MainResponse> call, Response<MainResponse> response) {
if(response == null){
Log.d("place","place id null");
}
else {
for (int i =0; i < response.body().getResults().size(); i++){
String place_id = response.body().getResults().get(i).getPlaceId();
Log.d("place",place_id);
}
}
}
这也是我的MainResponse pojo类
public class MainResponse {
@SerializedName("html_attributions")
@Expose
private List<Object> html_attributions = new ArrayList<Object>();
@SerializedName("next_page_token")
@Expose
private String next_page_token;
@SerializedName("results")
@Expose
private List<Result> results = new ArrayList<Result>();
@SerializedName("status")
@Expose
private String status;
public List<Object> getHtml_attributions() {
return html_attributions;
}
public void setHtml_attributions(List<Object> html_attributions) {
this.html_attributions = html_attributions;
}
public String getNext_page_token() {
return next_page_token;
}
public void setNext_page_token(String next_page_token) {
this.next_page_token = next_page_token;
}
public List<Result> getResults() {
return results;
}
public void setResults(List<Result> results) {
this.results = results;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
答案 0 :(得分:1)
我找到了解决问题的方法。 据了解,改进了一些关于改造的技巧。
我将此声明为基本网址:
https://maps.googleapis.com/maps/api/place/
而不是:
https://maps.googleapis.com/
Retrofit会自动删除/ maps / place /并给我留下不完整的请求。 我通过记录响应我发现了字符串。 然后我得到了错误的URL作为响应,我做了更改,现在工作正常。
所以要改造,BASE URL IS BASE URL ......不再是