改造java.net.SocketTimeoutException

时间:2016-09-07 06:47:39

标签: android retrofit

改装电话:

 RestAdapter restAdapter = new RestAdapter.Builder()
        .setEndpoint("BASE URL")
        .setLogLevel(RestAdapter.LogLevel.FULL)
        .setClient(new OkClient(getOkHttpClient()))
        .build();

依赖关系:

compile 'com.squareup.okhttp:okhttp:2.7.2'
compile 'com.squareup.retrofit:retrofit:1.9.0'

错误: USER_ID = XXXX&安培; ITEM_ID = X&安培; item_quantity = 1&安培; ITEM_COST = XXXX&安培; pay_id =条纹&安培; process_fee = XXX&安培; TOTAL_COST = XXX&安培; is_nagotiation = 0&安培; negotiate_id =安培;提示= XXX&安培; card_id的= XXXX

  

09-07 11:45:08.628 23473-24373 / com.bridgellc.bridge D / Retrofit:--->   END HTTP(177字节正文)09-07 11:45:18.635   23473-24373 / com.bridgellc.bridge D / Retrofit:---- ERROR http:XXXXXX   09-07 11:45:18.650 23473-24373 / com.bridgellc.bridge D / Retrofit:   java.net.SocketTimeoutException

3 个答案:

答案 0 :(得分:3)

这取决于各种因素,其中一个是超时,如connectionTimeout等。如果您的服务器在超时内没有响应,它将抛出SocketTimeoutException。您可以尝试增加timeout

的默认okhttp
private static OkHttpClient okClient() {
        return new OkHttpClient.Builder()
                .connectTimeout(1, TimeUnit.MINUTES)
                .writeTimeout(1, TimeUnit.MINUTES)
                .readTimeout(1, TimeUnit.MINUTES)
                .build();
    }

并将其设置为Retrofit实例。

Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Util.APP_UPDATE)
                .client(RestClient.okClient())
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();

答案 1 :(得分:2)

尝试为 OkHttp2 客户端设置超时。首先创建一个 OkHttp2 客户端,

OkHttpClient client = new OkHttpClient.Builder()
client.setConnectTimeout(5, TimeUnit.MINUTES);
client.setReadTimeout(5, TimeUnit.MINUTES);
.build();

然后将其添加为 Retrofit

的默认客户端
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("BASE_URL")
.setLogLevel(RestAdapter.LogLevel.FULL)
.setClient(client).build();

答案 2 :(得分:1)

您可以通过在Android配置文件中添加android:vmSafeMode="true"来解决此问题。 有关上述问题的讨论可以在github中找到。 https://github.com/square/okhttp/issues/1771 https://github.com/square/okhttp/issues/1518#issuecomment-87996760