2

I am just trying to do post api call using Retrofit.The server is responding with correct data.I checked with Postman(Chrome). My code is as follows

public class MainActivity extends Activity implements retrofit2.Callback>{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


  final OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .connectTimeout(6, TimeUnit.MINUTES)
            .readTimeout(6, TimeUnit.MINUTES)
            .writeTimeout(6, TimeUnit.MINUTES)
            .build();


    Gson gson = new GsonBuilder()
            .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
            .create();
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://kokanplaces.com/")
            .addConverterFactory(GsonConverterFactory.create(gson)).client(okHttpClient)
            .build();

    // prepare call in Retrofit 2.0

    ApiInterface apiService =
            ApiClient.getClient().create(ApiInterface.class);

    Call<List<CityModel>> call = apiService.getCitiesList();;
    //asynchronous call
    call.enqueue(this);
}


@Override
public void onResponse(Call<List<CityModel>> call, Response<List<CityModel>> response) {
    int code = response.code();
    if (code == 200) {
         for (CityModel cityModel : response.body()) {
               System.out.println(
                        cityModel.getCityname() + " (" + cityModel.getCityId() + ")");
            }           


    } else {
        Toast.makeText(this, "Did not work: " + String.valueOf(code), Toast.LENGTH_LONG).show();
    }

}

@Override
public void onFailure(Call<List<CityModel>> call, Throwable t) {
    Toast.makeText(this, "failure", Toast.LENGTH_LONG).show();
    System.out.println(t.fillInStackTrace());
    t.printStackTrace();

}

}

public interface ApiInterface {
@POST("wp-json/getCities")
Call<List<CityModel>> getCitiesList();

}

Every time it is throwing Socket timeout exception.

Any solution will be great help.

2 Answers 2

1

I met the problems like you before. I fixed by adding custom OkHttpClient:

Constants.TIMEOUT_CONNECTION = 60;
private OkHttpClient getOkHttpClient() {
        final OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .readTimeout(0, TimeUnit.NANOSECONDS)
                .connectTimeout(Constants.TIMEOUT_CONNECTION, TimeUnit.SECONDS)
                .writeTimeout(Constants.TIMEOUT_CONNECTION, TimeUnit.SECONDS)
//                .sslSocketFactory(getSSLSocketFactory())
                .build();
        return okHttpClient;
    }

and retrofitAdapter:

retrofitAdapter = new Retrofit.Builder()
                        .baseUrl(ConstantApi.BASE_URL)
                        .addConverterFactory(GsonConverterFactory.create(gson))
                        .client(getOkHttpClient())
                        .build();

Remember readTimeout is 0, I am using retrofit 2.1.0. Default timeout of retrofit is 10 seconds. I tried to set readTimeout is 60 seconds but no effect.

Sign up to request clarification or add additional context in comments.

2 Comments

I found my mistake that
instead of ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class); I need to call retrofit.create(ApiInterface.class); Other things were fine.
0

Topic tags: Socket closed, socket timeout

Explanation: Retrofit maintains connection which is locking socket.

More: https://github.com/square/okhttp/issues/3146

SOLUTION: configure connectionPool like in below example:

private OkHttpClient getOkHttpClient() {
    final OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .connectionPool(new ConnectionPool(0,1,TimeUnit.NANOSECONDS))
            .build();
    return okHttpClient;
}

Please remember to mark answer as correct :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.