0

I am trying to get a response in 30 seconds but I am not able to get it in time. I am using Retrofit 2 to make an API call. In Postman checking with emailId and password getting success response. My application also getting success response but after success, activity is not moving to next activity.

Can anyone help me with Retrofit to set a timeout.

String url = "xxxxxx";
Retrofit retrofit = null;
Log.d("123", "retrofit");

if (retrofit == null) {
    retrofit = new Retrofit.Builder().baseUrl(url).addConverterFactory(GsonConverterFactory.create()).build();
    Log.d("123", "build();");
}

final ProgressDialog dialog = new ProgressDialog(LoginActivity.this);
dialog.setMessage("Authenticating...." + 30000 / 1000 + " Second(s)");
dialog.setIndeterminate(false);

new CountDownTimer(30000, 1000) {

    public void onTick(long millisUntilFinished) {
        // You don't need anything here
        dialog.setMessage("Authenticating....");
        if (!dialog.isShowing()) dialog.show();
    }

    public void onFinish() {
        if (dialog.isShowing()) dialog.dismiss();
    }
}.start();

API1 service = retrofit.create(API1.class);
Call<Login> call = service.authenticate(emailId, password);
Log.i(TAG, "Sending---" + url + service + url + "\n" + "emailId:" + emailId + "\n" + "password:" + password);

call.enqueue(new Callback<Login>() {

    @Override
    public void onResponse(Call<Login> call, Response<Login> response) {

        if (response != null && response.isSuccessful() && response.code() == 200) {
            String status = response.body().getStatus().toString();
            Log.i(status, "success");
            if (status.equals("success")) {
                // dialog.dismiss();
                Toast.makeText(LoginActivity.this, "Login successfully", Toast.LENGTH_SHORT).show();
                Intent mainIntent;
                mainIntent = new Intent(LoginActivity.this, DeviceControlActivity.class);
                startActivity(mainIntent);
                finish();
            } else {
                Toast.makeText(LoginActivity.this, "No Response from the server", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(LoginActivity.this, "Invalid EmailId and password", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onFailure(Call<Login> call, Throwable t) {
        // Toast.makeText(LoginActivity.this, "Some error occurred -> ", Toast.LENGTH_LONG).show();;
        // dialog.dismiss();
    }
});
Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112
jyo cha
  • 71
  • 9
  • **Please don't repeat questions.** Simply editing this post with any new information you had, any new code you'd tried, or an explanation of why any posted answers didn't work, would've bumped it to the top of the active queue. I've closed this as a duplicate of the newer one, since you seem to have gotten your solution there, but, in the future, please just edit the original. – AskNilesh Jan 22 '19 at 06:04
  • 1
    @IlmariKaronen i have updated the duplicate target question check again – AskNilesh Feb 19 '19 at 13:04

1 Answers1

2

You can set the read and connect timeouts using Okhttp.

You will need to add a dependency in build.gradle

implementation 'com.squareup.okhttp3:okhttp:3.12.1'

EXAMPLE

OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
        .connectTimeout(40, TimeUnit.SECONDS)
        .readTimeout(60, TimeUnit.SECONDS)
        .writeTimeout(60, TimeUnit.SECONDS)
        .build();

Retrofit retrofit = new Retrofit.Builder()
        .client(okHttpClient)
        .baseUrl(Constants.WEB_SERVICE)
        .addConverterFactory(GsonConverterFactory.create(gson))
        .build();
Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112
Milind Mevada
  • 3,145
  • 1
  • 14
  • 22