gpt4 book ai didi

java - 使用 retrofit2.0 进行变量替换的 post 方法动态 url 期间出现 NetworkOnMainThreadException

转载 作者:搜寻专家 更新时间:2023-11-01 08:24:22 25 4
gpt4 key购买 nike

我有动态变量替换的帖子 url。所以在搜索许多链接后,我为相同的代码编写了以下代码。

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

我通过以下方式调用它

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

不幸的是,我的应用程序停止并给出以下异常

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)

相同的@Path 变量对于 GET 工作正常

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

但不适用于邮寄。请帮忙。

最佳答案

对于运行时间更长的任务,请使用 enqueue它在后台运行 Asynchronously,而不是 execute这是一个同步进程,所以使用

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

关于java - 使用 retrofit2.0 进行变量替换的 post 方法动态 url 期间出现 NetworkOnMainThreadException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46767902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com