我是Android的新手,并试图在github上使用Retrofit2和基本的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"}}
]
我不知道我做错了什么。