java.net.UnknownHostException:无法解析主机“my_base_server_url”:没有与主机名关联的地址

时间:2018-06-01 15:39:40

标签: android retrofit2 okhttp

通常,有些用户会遇到此异常,我无法确定可能性如何(即本周160位用户在1000位用户身上遇到此问题):

java.net.InetAddress.lookupHostByName (InetAddress.java:441)
java.net.InetAddress.getAllByName (InetAddress.java:215)
okhttp3.Dns$1.lookup (Dns.java:40)
okhttp3.internal.connection.RouteSelector.resetNextInetSocketAddress (RouteSelector.java:185)
okhttp3.internal.connection.RouteSelector.nextProxy (RouteSelector.java:149)
okhttp3.internal.connection.RouteSelector.next (RouteSelector.java:84)
okhttp3.internal.connection.StreamAllocation.findConnection (StreamAllocation.java:214)
okhttp3.internal.connection.StreamAllocation.findHealthyConnection (StreamAllocation.java:135)
okhttp3.internal.connection.StreamAllocation.newStream (StreamAllocation.java:114)
okhttp3.internal.connection.ConnectInterceptor.intercept (ConnectInterceptor.java:42)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:147)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:121)
okhttp3.internal.cache.CacheInterceptor.intercept (CacheInterceptor.java:93)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:147)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:121)
okhttp3.internal.http.BridgeInterceptor.intercept (BridgeInterceptor.java:93)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:147)
okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept (RetryAndFollowUpInterceptor.java:126)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:147)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:121)
com.myapp.network.retrofit.RestCoreRetrofit$1.intercept (RestCoreRetrofit.java:117)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:147)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:121)
okhttp3.RealCall.getResponseWithInterceptorChain (RealCall.java:200)
okhttp3.RealCall.execute (RealCall.java:77)
retrofit2.OkHttpCall.execute (OkHttpCall.java:180)
retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall.execute (ExecutorCallAdapterFactory.java:91)
com.myapp.network.retrofit.RestApiBaseRetrofit.onSyncResponse (RestApiBaseRetrofit.java:59)

拦截方法:

public static OkHttpClient.Builder getInitialOkHttpClientBuilder() {
        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

        // Setup timeouts
        httpClient.connectTimeout(60, TimeUnit.SECONDS);
        httpClient.readTimeout(120, TimeUnit.SECONDS);
        httpClient.writeTimeout(120, TimeUnit.SECONDS);

        return httpClient;
    }

    public Retrofit getClient() {
        if (mRetrofit == null) {
            OkHttpClient.Builder httpClient = getInitialOkHttpClientBuilder();

            // Setup http header parameters
            httpClient.addInterceptor(new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Builder ongoing = chain.request().newBuilder();
                    ongoing.addHeader("Accept", "application/json;versions=1");

                    // User Agent
                    ongoing.addHeader("User-Agent", HelpUtils.getUserAgent());

                    // Timezone

                    TimeZone tz = TimeZone.getDefault();
                    if (tz != null) {
                        ongoing.addHeader("Time-Zone", tz.getID());
                    }

                    // Locale

                    // Currently the locale information is formatted by Android SDK like: fr_CA
                    String locale = CompatUtils.getLocaleToString();
                    // But In the header HTTP, the correct format is fr-CA (see https://en.wikipedia.org/wiki/IETF_language_tag)
                    if (locale.contains("_")) {
                        locale = locale.replace('_', '-');
                    }

                    ongoing.addHeader("Accept-Language", locale);

                    // Time-format

                    ongoing.addHeader("Time-Format",
                            android.text.format.DateFormat.is24HourFormat(MyApplication.getInstance()) ? "24h" : "12h");

                    // Authorization

                    String userAccessToken = MyApplication.getInstance().getDataHolder().getUserAccessToken();
                    if (!TextUtils.isEmpty(userAccessToken)) {
                        ongoing.addHeader("Authorization", Defines.OAUTH_AUTHENTIFICATION_TOKEN_SUFFIX + userAccessToken);
                    }

                    // Device id

                    ongoing.addHeader("Device-Id", HelpUtils.getDeviceID());

                    return chain.proceed(ongoing.build());
                }
            });

            mRetrofit = new Retrofit.Builder()
                    .baseUrl(com.myapp.BuildConfig.SERVER_URL + "/")
                    .addConverterFactory(GsonConverterFactory.create(GsonUtils.createGsonBuilder()))
//                    .client(enableTls12(httpClient).build())
                    .client(httpClient.build())
                    .build();
        }

        return mRetrofit;
    }

你有什么想法吗?

非常感谢你们!

0 个答案:

没有答案