gpt4 book ai didi

android - SocketTimeoutException android改造

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:12:18 25 4
gpt4 key购买 nike

我正在我的 android 应用程序中使用改造构建一个 Restful 客户端,但是我在尝试从我的网络服务中检索数据时遇到异常,第一次调用毫无异常(exception)地工作,但我更改了参数并重试获取新数据我得到下面提到的异常

这是我的服务生成器类

public class ServiceGenerator {

public static final String API_BASE_URL = "http://192.168.43.109:9988/Serv/";

private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();



private static Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create());

public static <S> S createService(Class<S> serviceClass) {
Retrofit retrofit = builder.client(httpClient.build()).build();
return retrofit.create(serviceClass);
}}

这是我得到的堆栈跟踪:

W/System.err: java.net.SocketTimeoutException
W/System.err: at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
W/System.err: at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:37)
W/System.err: at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:237)
W/System.err: at okio.Okio$2.read(Okio.java:140)
W/System.err: at okio.AsyncTimeout$2.read(AsyncTimeout.java:238)
W/System.err: at okio.RealBufferedSource.indexOf(RealBufferedSource.java:325)
W/System.err: at okio.RealBufferedSource.indexOf(RealBufferedSource.java:314)
W/System.err: at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:210)
W/System.err: at okhttp3.internal.http.Http1xStream.readResponse(Http1xStream.java:184)
W/System.err: at okhttp3.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:125)
W/System.err: at okhttp3.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:775)
W/System.err: at okhttp3.internal.http.HttpEngine.access$200(HttpEngine.java:86)
W/System.err: at okhttp3.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:760)
W/System.err: at okhttp3.internal.http.HttpEngine.readResponse(HttpEngine.java:613)
W/System.err: at okhttp3.RealCall.getResponse(RealCall.java:244)
W/System.err: at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:201)
W/System.err: at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:163)
W/System.err: at okhttp3.RealCall.execute(RealCall.java:57)
W/System.err: at retrofit2.OkHttpCall.execute(OkHttpCall.java:174)
W/System.err: at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall.execute(ExecutorCallAdapterFactory.java:89)
W/System.err: at com.example.admin.theapp.ListPersonne$GetPersonnesAsyncTask.doInBackground(ListPersonne.java:418)
W/System.err: at com.example.admin.theapp.ListPersonne$GetPersonnesAsyncTask.doInBackground(ListPersonne.java:409)
W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:288)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
W/System.err: at java.lang.Thread.run(Thread.java:818)
W/System.err: java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference
W/System.err: at com.example.admin.theapp.ListPersonne.onCreate(ListPersonne.java:143)
W/System.err: at android.app.Activity.performCreate(Activity.java:6374)
W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2743)
W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2855)
W/System.err: at android.app.ActivityThread.access$900(ActivityThread.java:181)
W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1474)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err: at android.os.Looper.loop(Looper.java:145)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6117)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

最佳答案

private OkHttpClient getClient() {
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(5, TimeUnit.MINUTES)
.readTimeout(5, TimeUnit.MINUTES)
.build();
return client;
}

关于android - SocketTimeoutException android改造,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39573308/

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