M
M
Maxim UU2016-09-26 23:05:00
Android
Maxim UU, 2016-09-26 23:05:00

What is the cause of the error on retrofit 2 synchronous request?

Hello!
API request description:

public interface APIv1 {

    @Multipart
    @POST("/api/v1/order/create")
    Call<ResponseBody> SendOrder(@PartMap Map<String, RequestBody> params);

}

When the request is executed asynchronously, everything works fine:
call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {

                if (response.isSuccessful()) {
                    Map<String, String> map = gson.fromJson(response.body().toString(), Map.class);

                    for (Map.Entry e : map.entrySet()) {
                        System.out.println(e.getKey() + " " + e.getValue());
                        Log.e("Delivery message:", e.getKey() + " " + e.getValue());
                    }

                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.e("Upload error:", t.getMessage());
            }
        });

But when trying to execute the query synchronously:
try {
            retrofit2.Response<ResponseBody> response = call.execute();

            if(response.isSuccessful()) {
                
                return true;
            }


        } catch (IOException e) {
            e.printStackTrace();
        }

Application crashes:
FATAL EXCEPTION: main
 Process: ru.serv_techno.delivery_st, PID: 3973
 android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1162)
at java.net.InetAddress.lookupHostByName(InetAddress.java:425)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:259)
at java.net.InetAddress.getAllByName(InetAddress.java:221)
at okhttp3.Dns$1.lookup(Dns.java:39)
at okhttp3.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:173)
at okhttp3.internal.http.RouteSelector.nextProxy(RouteSelector.java:139)
at okhttp3.internal.http.RouteSelector.next(RouteSelector.java:81)
at okhttp3.internal.http.StreamAllocation.findConnection(StreamAllocation.java:172)
at okhttp3.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:123)
at okhttp3.internal.http.StreamAllocation.newStream(StreamAllocation.java:93)
at okhttp3.internal.http.HttpEngine.connect(HttpEngine.java:296)
at okhttp3.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
at okhttp3.RealCall.getResponse(RealCall.java:243)
at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:201)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:163)
at okhttp3.RealCall.execute(RealCall.java:57)
at retrofit2.OkHttpCall.execute(OkHttpCall.java:174)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall.execute(ExecutorCallAdapterFactory.java:89)
at ru.serv_techno.delivery_st.MyOrder.sendOrder(MyOrder.java:154)
at ru.serv_techno.delivery_st.BoxActivity.CreateOrder(BoxActivity.java:170)
at ru.serv_techno.delivery_st.BoxActivity.doPositiveClick(BoxActivity.java:98)
at ru.serv_techno.delivery_st.MyAlert$2.onClick(MyAlert.java:45)
at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:157)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5674)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:963)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:758)

Prompt, who can in what the reason.
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2016-09-27
@morotkin

Everything is written in the stack trace. NetworkOnMainThreadException. Have you tried googling? In Android, you can not go to the network on the main thread.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question