【发布时间】:2019-06-15 13:26:19
【问题描述】:
我试图在 30 秒内得到回复,但我无法及时得到回复。我正在使用 Retrofit 2 进行 API 调用。在Postman 检查emailId 和password 获得成功响应。我的应用程序也获得了成功响应,但成功后,活动不会移动到下一个活动。
谁能帮我用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 我再次更新了重复目标问题检查