2017-10-16 20 views
2

J'ai l'URL de post pour les substitutions de variables dynamiques. Ainsi, après la recherche dans plusieurs liens, j'ai créé le code suivant pour le même.NetworkOnMainThreadException lors de l'URL dynamique de la méthode post utilisant retrofit2.0 pour la substitution de variables

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

et je fais appel cela en suivant façon

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(); 
    } 

Mon application est malheureusement stoping et en donnant l'exception suivante

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) 

même variable @Path fonctionne très bien pour GET

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

mais pas pour la poste. S'il vous plaît aider.

Répondre

3

Pour tâche en cours plus l'utilisation enqueue qui fonctionne Asynchronously en arrière-plan, au lieu de execute qui est un processus synchrone, utilisez donc

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) { 
      } 
     }); 
+0

Merci beaucoup :-) – ki123

+0

je suis heureux que je pourrais aider, heureux de codage –