【问题标题】:how to set timer in android using retrofit in android using retrofit2 [duplicate]如何在android中使用retrofit在android中使用retrofit2设置计时器[重复]
【发布时间】:2019-06-15 13:26:19
【问题描述】:

我试图在 30 秒内得到回复,但我无法及时得到回复。我正在使用 Retrofit 2 进行 API 调用。在Postman 检查emailIdpassword 获得成功响应。我的应用程序也获得了成功响应,但成功后,活动不会移动到下一个活动。

谁能帮我用Retrofit设置超时。

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();
    }
});

【问题讨论】:

  • 请不要重复问题。 只需使用您拥有的任何新信息、您尝试过的任何新代码或任何已发布答案的原因的解释来编辑此帖子不工作,会把它撞到活动队列的顶部。我已将其作为新版本的副本关闭,因为您似乎已经在那里找到了解决方案,但是,在未来,请只编辑原始版本。
  • @IlmariKaronen 我再次更新了重复目标问题检查

标签: android retrofit2


【解决方案1】:

您可以使用Okhttp 设置读取和连接超时。

您需要在build.gradle中添加一个依赖项

实现'com.squareup.okhttp3:okhttp:3.12.1'

示例

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();

【讨论】:

  • 显示错误 newBuilder()
  • 更新了答案。请检查
  • 添加这个定时器后控制器是否会进入这个类 DeviceControlActivity.class
  • 如果你得到 Success 响应,它就会去,它基本上增加了连接/读取超时。其他逻辑由你决定
猜你喜欢
  • 2012-03-20
  • 2016-09-06
  • 2011-06-03
  • 1970-01-01
  • 2019-05-18
  • 1970-01-01
  • 2021-12-12
相关资源
最近更新 更多