我是 Android 新手,正在尝试将 Retrofit2 与 github 上的基本 SimpleService.java 一起使用。该示例运行良好,但在使用 myURL 时,我得到NullPointerException
:
Thread thread = new Thread(new Runnable(){
@Override
public void run(){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(myURL)
.addConverterFactory(GsonConverterFactory.create())
.build();
// Create an instance of our GitHub API interface.
SimpleService.GitHub github = retrofit.create(SimpleService.GitHub.class);
// Create a call instance for looking up Retrofit contributors.
Call<List<SimpleService.Pending>> call2 = github.getPendings();
List<SimpleService.Pending> pendings = null;
try {
pendings = call2.execute().body();
} catch (IOException e) {
e.printStackTrace();
}
for (SimpleService.Pending pending : pendings) {
System.out.println(pending.idpr + " ;;; " + pending.lastlat + " ;;; " + pending.lastlng);
}
}
});
thread.start();
块中的代码try
返回 null 调试器说。这是我的界面(没有更改名称,仍然像示例中的 GitHub)和SimpleService
类:
public final class SimpleService {
public static final String API_URL = myURL;
public static class Pending {
public final int idpr;
public final double lastlat;
public final double lastlng;
public Pending(int idpr, double lastlat, double lastlng) {
this.idpr = idpr;
this.lastlat = lastlat;
this.lastlng = lastlng;
}
}
public interface GitHub {
@GET("/repos/{owner}/{repo}/contributors")
Call<List<Contributor>> contributors(
@Path("owner") String owner,
@Path("repo") String repo);
@GET("/pendings")
Call<List<Pending>> getPendings();
}
这是我收到的典型 json:
[
{"idpr":1,"lastlat":48.788986,"lastlng":7.24545,"idu":{"username":"foulekparo"}},
{"idpr":2,"lastlat":48.803717,"lastlng":6.47526,"idu":{"username":"flouka"}}
]
我不知道我做错了什么。