Prithiv Dharmaraj
Prithiv Dharmaraj

Reputation: 357

Retrofit java.net.SocketTimeoutException

Retrofit Call:

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

Dependencies:

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

Error: user_id=XXXX&item_id=X&item_quantity=1&item_cost=XXXX&pay_id=stripe&process_fee=XXX&total_cost=XXX&is_nagotiation=0&negotiate_id=&tips=XXX&card_id=XXXX

09-07 11:45:08.628 23473-24373/com.bridgellc.bridge D/Retrofit: ---> END HTTP (177-byte body) 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

Upvotes: 4

Views: 11038

Answers (3)

Prokash Sarkar
Prokash Sarkar

Reputation: 11873

Try to set a timeout for your OkHttp2 Client. First create an OkHttp2 client,

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

Then add it as the default client to the Retrofit

RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("BASE_URL")
.setLogLevel(RestAdapter.LogLevel.FULL)
.setClient(client).build();

Upvotes: 2

Godfather
Godfather

Reputation: 848

It depends on various factor one of those is timeout like connectionTimeout etc.If your server is not responding within timeout it will throw SocketTimeoutException. you can try increasing default timeout of okhttp

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

and set it to Retrofit instance.

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

Upvotes: 4

Alex Chengalan
Alex Chengalan

Reputation: 8281

You can fix this issue by adding android:vmSafeMode="true" in the Android configuration file. There is discussion on the above problem can be found in github. https://github.com/square/okhttp/issues/1771 https://github.com/square/okhttp/issues/1518#issuecomment-87996760

Upvotes: 1

Related Questions