SocketTimeoutException android 改造
SocketTimeoutException android retrofit
我正在我的 android 应用程序中使用改装构建一个 restful 客户端,但是我在尝试从我的网络服务中检索数据时遇到异常,
第一次调用毫无例外地工作,但我更改了参数并重试获取新数据我得到下面提到的异常
这是我的服务生成器class
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[=12=]0(PlainSocketImpl.java:37)
W/System.err: at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:237)
W/System.err: at okio.Okio.read(Okio.java:140)
W/System.err: at okio.AsyncTimeout.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.access0(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.call(AsyncTask.java:288)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err: at android.os.AsyncTask$SerialExecutor.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.access0(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;
}
如您所见,错误是java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference
。这可能是因为当服务器没有响应或抛出其他错误时,您尝试迭代到一个空的 Persons 列表。检查您的回调侦听器或 Observable(如果您使用的是 RxJava)。
我正在我的 android 应用程序中使用改装构建一个 restful 客户端,但是我在尝试从我的网络服务中检索数据时遇到异常, 第一次调用毫无例外地工作,但我更改了参数并重试获取新数据我得到下面提到的异常
这是我的服务生成器class
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[=12=]0(PlainSocketImpl.java:37)
W/System.err: at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:237)
W/System.err: at okio.Okio.read(Okio.java:140)
W/System.err: at okio.AsyncTimeout.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.access0(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.call(AsyncTask.java:288)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err: at android.os.AsyncTask$SerialExecutor.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.access0(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;
}
如您所见,错误是java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference
。这可能是因为当服务器没有响应或抛出其他错误时,您尝试迭代到一个空的 Persons 列表。检查您的回调侦听器或 Observable(如果您使用的是 RxJava)。