Tim Kruichkov
Tim Kruichkov

Reputation: 1493

retrofit.RetrofitError in Android project

I'm new in Retrofit. Can't make request without receiving retrofit.RetrofitError and NetworkOnMainThreadException when try:

RestAdapter restAdapter = new RestAdapter.Builder()
            .setEndpoint(Url.BASE_URL)
            .build();

    Api api = restAdapter.create(Api.class);
        for(Url url : Url.values()) {
            Journal journal = api.getJournalInfo(url.getUrl());      //Here error
            System.out.println("journal : " + journal.getTitle());
        }

Here is class Journal:

public class Journal {
private String title;
private String description;
private String image;

public String getTitle() {
    return title;
}
}

class Url:

public enum Url {
ECONAMICS("economics.json"),
PHYSICS("mathematical.json"),
MATHEMATICAL("physics.json");

public static final String BASE_URL = "https://dl.dropboxusercontent.com/u/17192683/rest/journals/";

private String url;
Url(String url) {
    this.url = url;
}

public String getUrl() {
    return url;
}
}

interface Api:

public interface Api {
@POST("/{url}")
public Journal getJournalInfo(@Path("url") String urlJournal);
}

Full exception:

03-21 23:44:43.824  22782-22782/tim_kriuchkov.com.task E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{tim_kriuchkov.com.task/tim_kriuchkov.com.task.MainActivity}: retrofit.RetrofitError
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
        at android.app.ActivityThread.access$600(ActivityThread.java:162)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:194)
        at android.app.ActivityThread.main(ActivityThread.java:5371)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: retrofit.RetrofitError
        at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:400)
        at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
        at $Proxy0.getJournalInfo(Native Method)
        at tim_kriuchkov.com.task.MainActivity.onCreate(MainActivity.java:66)  // here error
        at android.app.Activity.performCreate(Activity.java:5122)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)

next part of exception:

 Caused by: android.os.NetworkOnMainThreadException
        at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1128)
        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:340)
        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:316)
        at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:469)
        at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433)
        at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
        at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
        at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:292)
        at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:505)
        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:326)

and last part:

at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
at $Proxy0.getJournalInfo(Native Method)
at tim_kriuchkov.com.task.MainActivity.onCreate(MainActivity.java:66)  // here error

Would you help me? I've spent a lot of time...

Upvotes: 0

Views: 4214

Answers (1)

sockeqwe
sockeqwe

Reputation: 15929

You run your http call not in a separated Thread, but on the main android UI thread which is forbidden by android os (hence the exception).

Change your Api interface to use Retrofits Callback. That will execute your request async and will invoke the callback on success or on error.

public interface Api {
   @POST("/{url}")
   public void getJournalInfo(@Path("url") String urlJournal, Callback<Journal> callback);
}

Upvotes: 6

Related Questions