我正在使用我的Android应用程序中的改造来构建一个宁静的客户端,但是当我尝试从我的web服务中检索数据时,我正在努力解决异常问题, 第一个调用没有例外,但我更改了参数并重试获取新数据我得到了下面提到的异常
这是我的服务生成器类
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)
答案 0 :(得分:9)
private OkHttpClient getClient() {
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(5, TimeUnit.MINUTES)
.readTimeout(5, TimeUnit.MINUTES)
.build();
return client;
}
答案 1 :(得分:0)
如您所见,错误为java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference
。这很可能是当服务器没有响应或抛出其他错误时,您尝试迭代到人员的空列表。检查你的Callback监听器或Observable(如果你使用的是RxJava)。