简体   繁体   中英

NetworkOnMainThreadException during post method dynamic url using retrofit2.0 for variable substitution

I have post url for dynamic variable substitutions.So after searching in many links I made my following code for the same.

@POST("clients/{clientId}/devices/{deviceCode}/authenticate")
    Call<String> isAuthenticate(@Path("clientId") Long 
    clientId,@Path("deviceCode") String deviceCode);

and I am calling this by following way

Call<String> res = webservice.isAuthenticate((long) 2,strDeviceCode);
    try {
        int result = res.execute().code();
        //Toast.makeText(this, "is executed "+res.isExecuted(), Toast.LENGTH_SHORT).show();
        Toast.makeText(this, ""+result, Toast.LENGTH_SHORT).show();
    } catch (IOException e) {
        e.printStackTrace();
    }

My app is unfortunately stoping and giving the following exception

android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1317)
at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:86)
at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:74)
at java.net.InetAddress.getAllByName(InetAddress.java:752)
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 com.example.kiran.kioskapp.activity.LoginActivity.checkAuthentication(LoginActivity.java:74)
at com.example.kiran.kioskapp.activity.LoginActivity.access$000(LoginActivity.java:37)
at com.example.kiran.kioskapp.activity.LoginActivity$1.onClick(LoginActivity.java:64)
at android.view.View.performClick(View.java:5612)
at android.view.View$PerformClick.run(View.java:22285)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)

same @Path variable is working fine for GET

@GET("clients/{clientId}/kiosk-settings")
    Call<ModelUserDetails> getDetails(@Path("clientId") String clientId);

but not for post. Please help.

For longer running task use enqueue which runs Asynchronously in the background, instead of execute which is a Synchronous process, so use

 res.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String>call, Response<String> response) {
              // response.body(); // to get the response
            }

            @Override
            public void onFailure(Call<String>call, Throwable t) {
            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM