改进致命异常:OkHttp Dispatcher,java.lang.NullPointerException:拦截器

时间:2018-05-01 05:46:52

标签: android retrofit interceptor okhttp

如果没有互联网或返回null,应用程序始终会崩溃。 以下是日志

FATAL EXCEPTION: OkHttp Dispatcher
Process: , PID: 24400
java.lang.NullPointerException: interceptor 
RestClient$1@301dd345 returned null
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:157)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:147)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)

这是Log

05-01 09:17:37.920 24400-24439/? D/OkHttp: --> END GET
05-01 09:17:37.922 24400-24439/? D/OkHttp: <-- HTTP FAILED: java.net.UnknownHostException: Unable to resolve host "": No address associated with hostname
05-01 09:17:37.923 24400-24439/? W/System.err: java.net.UnknownHostException: Unable to resolve host "": No address associated with hostname
05-01 09:17:37.923 24400-24439/? W/System.err:     at java.net.InetAddress.lookupHostByName(InetAddress.java:427)
05-01 09:17:37.924 24400-24439/? W/System.err:     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
05-01 09:17:37.924 24400-24439/? W/System.err:     at java.net.InetAddress.getAllByName(InetAddress.java:215)
05-01 09:17:37.924 24400-24439/? W/System.err:     at okhttp3.Dns$1.lookup(Dns.java:39)
05-01 09:17:37.924 24400-24439/? W/System.err:     at okhttp3.internal.connection.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:185)
05-01 09:17:37.924 24400-24439/? W/System.err:     at okhttp3.internal.connection.RouteSelector.nextProxy(RouteSelector.java:149)
05-01 09:17:37.925 24400-24439/? W/System.err:     at okhttp3.internal.connection.RouteSelector.next(RouteSelector.java:84)
05-01 09:17:37.925 24400-24439/? W/System.err:     at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:213)
05-01 09:17:37.927 24400-24439/? W/System.err:     at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:134)
05-01 09:17:37.927 24400-24439/? W/System.err:     at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:113)
05-01 09:17:37.927 24400-24439/? W/System.err:     at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
05-01 09:17:37.927 24400-24439/? W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
05-01 09:17:37.927 24400-24439/? W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
05-01 09:17:37.928 24400-24439/? W/System.err:     at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
05-01 09:17:37.928 24400-24439/? W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
05-01 09:17:37.928 24400-24439/? W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
05-01 09:17:37.928 24400-24439/? W/System.err:     at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
05-01 09:17:37.928 24400-24439/? W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
05-01 09:17:37.928 24400-24439/? W/System.err:     at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:125)
05-01 09:17:37.929 24400-24439/? W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
05-01 09:17:37.929 24400-24439/? W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
05-01 09:17:37.929 24400-24439/? W/System.err:     at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:212)
05-01 09:17:37.929 24400-24439/? W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
05-01 09:17:37.929 24400-24439/? W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
05-01 09:17:37.929 24400-24439/? W/System.err:     at RestClient$1.intercept(RestClient.java:66)
05-01 09:17:37.929 24400-24439/? W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
05-01 09:17:37.929 24400-24439/? W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
05-01 09:17:37.929 24400-24439/? W/System.err:     at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
05-01 09:17:37.929 24400-24439/? W/System.err:     at okhttp3.RealCall$AsyncCall.execute(RealCall.java:147)
05-01 09:17:37.930 24400-24439/? W/System.err:     at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
05-01 09:17:37.930 24400-24439/? W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
05-01 09:17:37.930 24400-24439/? W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
05-01 09:17:37.930 24400-24439/? W/System.err:     at java.lang.Thread.run(Thread.java:818)

我的网络类代码。我在这里提出要求。 java.lang.NullPointerException:拦截器RestClient $ 1 @ 301dd345返回null 同一序列中的另一个日志显示此消息 D / OkHttp:&lt; - HTTP FAILED:java.net.UnknownHostException:无法解析主机“example.com”:没有与主机名关联的地址

public static String API_BASE_URL = "";
    public static final String BASE_URL_QA = "https://example.com";
    public static final String BASE_URL_LIVE = "https://example.com";
    private Retrofit builder;
    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
    private Response response;

    public RestClient() {
        if (SharedPreferenceManager.getSharedInstance().getStringFromPreferances(Constants.SharedPreferenceKeys.QA.getKey()).equals("yes")) {
           BASE_URL = BASE_URL_QA;
        } else {
           BASE_URL = BASE_URL_LIVE;
        }
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        if (!AppData.isTrue) {
            httpClient.interceptors().add(new Interceptor() {
                @Override
                public okhttp3.Response intercept(Chain chain) throws IOException {
                    try {
                        Request original = chain.request();
                        // Request customization: add request headers
                        Request.Builder requestBuilder = original.newBuilder();

                        if (AppData.isBearer) {
                            String token = getAccessToken();
                            String bearer = getToken_type();

                            if (AppUtils.getAppUtils(null).isNullOrEmpty(token) && SharedPreferenceManager.getSharedInstance().getFromPreferences() != null) {
                                token = SharedPreferenceManager.getToken();
                                bearer = SharedPreferenceManager.getTokentype();
                            }

                            requestBuilder.addHeader("Authorization", bearer + " " + token);
                        }
                        requestBuilder.addHeader("key", "");
                        Request request = requestBuilder.build();
                        AppData.isTrue = false;

                        response = chain.proceed(request);
                    } catch (Exception e) {
                        e.printStackTrace();
                    } catch (Error e2) {
                        Log.e("", "ROME parse error2: " + e2.toString());
                    }
                    return response;
                }
            });
        }

        httpClient.addInterceptor(logging);
        OkHttpClient shortHttpClient = httpClient.readTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .connectTimeout(20, TimeUnit.SECONDS)
                .retryOnConnectionFailure(true)
                .build();

        builder = new Retrofit.Builder().
                baseUrl(BASE_URL).
                addConverterFactory(new ToStringConverterFactory()).
                addConverterFactory(GsonConverterFactory.create()).
                addCallAdapterFactory(RxJavaCallAdapterFactory.create()).
                client(shortHttpClient).
                build();
    }

    public <S> S createService(Class<S> serviceClass) {
        return builder.create(serviceClass);
    }

0 个答案:

没有答案