我试图在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();
}
});
答案 0 :(得分: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();