RetrofitError exception
I have the following code ...
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;
}
And don't print in the log and the app doesn't work ... there is an Exception part
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)
Does anyone know why? Thanks to
source to share
Retrofit has a mechanism for asynchronous callbacks without using AsyncTask
or service. Modify your interface to use a callback instead of the return type -
public interface GithubService {
@GET("/repos/{owner}/{repo}/contributors")
public void contributors(@Path("owner") String owner, @Path("repo") String repo,
Callback<List<Contributor>> contributors);
}
You can call it like this:
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
}
});
source to share
When I executed your code given the correct parameters passed to the method contributors(...)
, here is the complete 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
is used for description REST API
using java interface (like GitHubService), annotations and internally Gson
for parsing. You need to create a new thread using AsyncTask
IntentService or whatever suits your code, because network operations shouldn't be done on the UI thread.
Check this link: http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html
I highly recommend not using the following lines as a solution. Thread class or AsyncTask or IntentService for your code
Try adding these lines to the method onCreate()
just for the purpose test as it is not good practice
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Hope this helps!
source to share