我在我的android项目中使用了Retrofit库。对于某些请求,改造不会获得已发送请求的响应,甚至不会抛出TimeoutException,但我们的api日志显示响应已发送。这是我的代码:
APIClient:
public class ApiClient {
private static String baseUrl;
private static Retrofit sRetrofit = null;
private static boolean isURLChanged;
public static Retrofit getClient() {
if (sRetrofit == null || isURLChanged) {
final OkHttpClient okHttpClient = new OkHttpClient.Builder()
.retryOnConnectionFailure(true)
.readTimeout(120, TimeUnit.SECONDS)
.connectTimeout(120, TimeUnit.SECONDS)
.build();
isURLChanged = false;
sRetrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
}
return sRetrofit;
}
public static void changeIP(String url) {
baseUrl = url;
isURLChanged = true;
}
}
APIInterface:
public interface ApiInterface {
@POST("signin")
Call<LoginRegisterResponse> login(@Body Profile profile);
}
RetrofitRequest:
public void registerPerson(final Context context) {
Call<LoginRegisterResponse> call = apiService.register(Person.getInstance());
call.enqueue(new Callback<LoginRegisterResponse>() {
@Override
public void onResponse(Call<LoginRegisterResponse> call, Response<LoginRegisterResponse> response) {
if (response.isSuccessful()) {
...
} else {
...
}
}
@Override
public void onFailure(Call<LoginRegisterResponse> call, Throwable t) {
...
}
});
}
问题出在哪里?