RetrofitError异常

时间:2014-11-26 04:55:01

标签: android retrofit

我有以下代码......

public interface GithubService {
    @GET("/repos/{owner}/{repo}/contributors")
    public List<Contributor> contributors(@Path("owner") String owner, @Path("repo") String repo);
}

RepositoresBusinessController.class

RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint("https://api.github.com").build();
GithubService service = restAdapter.create(GithubService.class);
listContributors = service.contributors(username, "retrofit");
Log.d("List Contributors", listContributors+" ");

Contributor.class

public class Contributor {
    String login;
    int contributions;
}

并且不会在日志中打印并且应用程序会消失...有一部分异常

 725-725/com.example.githubapiexample E/AndroidRuntime﹕ FATAL EXCEPTION: main
 code here`retrofit.RetrofitError
 at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:395)
 at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)

有人知道为什么吗?感谢

2 个答案:

答案 0 :(得分:3)

Retrofit具有异步回调机制,无需使用AsyncTask或服务。更改您的界面以使用回调而不是返回类型 -

public interface GithubService {
    @GET("/repos/{owner}/{repo}/contributors")
    public void contributors(@Path("owner") String owner, @Path("repo") String repo,
            Callback<List<Contributor>> contributors);
}

您可以按以下方式调用它 -

service.contributors(username, project, new Callback<List<Contributor>>() {
            @Override
            public void success(List<Contributor> contributors, Response response) {
                // got the list of contributors
            }

            @Override
            public void failure(RetrofitError error) {
                // Code for when something went wrong 
            }
        });

答案 1 :(得分:1)

当我执行代码时考虑到传递给方法contributors(...)的正确params,这里是完整的logcat

Caused by: retrofit.RetrofitError
    at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:395)
    at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
    at $Proxy0.contributors(Native Method)
    at com.example.retrofitsample.RetrofitActivity.onCreate(RetrofitActivity.java:25)
    at android.app.Activity.performCreate(Activity.java:5008)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
    ... 11 more
Caused by: android.os.NetworkOnMainThreadException
    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
    at java.net.InetAddress.getAllByName(InetAddress.java:214)
    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
    at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
    at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
    at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
    at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
    at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:461)
    at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433)
    at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
    at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
    at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
    at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:486)
    at libcore.net.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:134)
    at retrofit.client.UrlConnectionClient.readResponse(UrlConnectionClient.java:73)
    at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:38)
    at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:321)
    ... 17 more

Retrofit library用于使用java接口(例如GitHubService),注释来描述REST API,并在内部使用Gson进行解析。 您需要使用AsyncTask,IntentService或适合您代码的任何内容生成新线程,因为不应在UI线程上执行网络操作。

点击此链接:http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

  

我强烈建议不要使用以下行作为解决方案使用Thread   您的代码的类或AsyncTask或IntentService

尝试在onCreate()方法中添加这些行仅用于检查目的,因为这不是一个好习惯

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

希望这会有所帮助!!