Retrofit: call.execute always throw an error

Created on 10 Sep 2015  Â·  6Comments  Â·  Source: square/retrofit

Hi,

I am using retrofit on Android. Why does call.execute always throw an error.
Here is one method from my service

``` [java]
@FormUrlEncoded
@POST("/auth/signin")
Call signIn(@Field("email") String email, @Field("password") password);

Doing this throw an error

Call call = userAuthentication.signIn("foo", "bar');
User user = call.execute.body();
```

Why do I always get an error ?

PS: I am new on Retrofit.
Thanks.

Most helpful comment

You cannot execute HTTP requests on Android's main thread synchronously. Either use enqueue() to spawn asynchronous requests or only call execute() when you are on a background thread.

All 6 comments

What error?

On Wed, Sep 9, 2015, 10:08 PM Knight Solidary [email protected]
wrote:

Hi,

I am using retrofit on Android. Why does call.execute always throw an
error.
Here is one method from my service

@FormUrlEncoded
@POST("/auth/signin")
Call signIn(@Field("email") String email, @Field("password") password);

Doing this throw an error

Call call = userAuthentication.signIn("foo", "bar');
User user = call.execute.body();

Why do I always get an error ?

PS: I am new on Retrofit.
Thanks.

—
Reply to this email directly or view it on GitHub
https://github.com/square/retrofit/issues/1073.

Here is the trace

ava.lang.RuntimeException: Unable to start activity ComponentInfo{com.tcheps/com.tcheps.activities.ProblemsFeedActivity}: android.os.NetworkOnMainThreadException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            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:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.os.NetworkOnMainThreadException
            at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
            at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
            at libcore.io.IoBridge.connectErrno(IoBridge.java:144)
            at libcore.io.IoBridge.connect(IoBridge.java:112)
            at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
            at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
            at java.net.Socket.connect(Socket.java:842)
            at com.squareup.okhttp.internal.Platform$Android.connectSocket(Platform.java:190)
            at com.squareup.okhttp.Connection.connectSocket(Connection.java:196)
            at com.squareup.okhttp.Connection.connect(Connection.java:172)
            at com.squareup.okhttp.Connection.connectAndSetOwner(Connection.java:367)
            at com.squareup.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:128)
            at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:328)
            at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:245)
            at com.squareup.okhttp.Call.getResponse(Call.java:267)
            at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:224)
            at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:195)
            at com.squareup.okhttp.Call.execute(Call.java:79)
            at retrofit.OkHttpCall.execute(OkHttpCall.java:112)

You cannot execute HTTP requests on Android's main thread synchronously. Either use enqueue() to spawn asynchronous requests or only call execute() when you are on a background thread.

I have the same problem, if you can provide a good example of a use call.execute() ?

thanks

"@Field("password") password" should be "@Field("password") String password"

Hi @ly1054

Here is one method from my service

@GET("movie/top_rated")
    Call<MoviesResponse> getTopRatedMoviesNew(@Query("api_key") String apiKey, @Query("page") int page);

Doing this throw an error

Call<MoviesResponse> call = apiService.getTopRatedMoviesNew(API_KEY,1);
MoviesResponse response = call.execute().body();
Was this page helpful?
0 / 5 - 0 ratings