Android 在onFailure中改装getMessage返回null
我在我的应用程序中使用这些库: 实现'com.squareup.Reformation2:Reformation:2.4.0' 实现'com.squareup.Reformation2:converter gson:2.3.0' 实现'com.squareup.okhttp3:okhttp:3.10.0' 这是我的客户端实例类实现:Android 在onFailure中改装getMessage返回null,android,api,retrofit2,Android,Api,Retrofit2,我在我的应用程序中使用这些库: 实现'com.squareup.Reformation2:Reformation:2.4.0' 实现'com.squareup.Reformation2:converter gson:2.3.0' 实现'com.squareup.okhttp3:okhttp:3.10.0' 这是我的客户端实例类实现: public class RetrofitClientInstance { private static Retrofit retrofit; p
public class RetrofitClientInstance {
private static Retrofit retrofit;
private static final String BASE_URL = "my basic url";
public static Retrofit getRetrofitInstance() {
OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.build();
if (retrofit == null) {
retrofit = new retrofit2.Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
}
return retrofit;
}
}
直到今天早上它都没有问题,但从今天早上开始它就不工作了,onFailure中的getMessage返回null,而t.toString()返回java.net.SocketTimeoutException。(邮递员还是可以的)
@覆盖
public void onFailure(@NonNull Call Call,@NonNull Throwable t){
call.clone().enqueue(此);
Log.e(“SplashScreen中的错误”,“!!”+t.getMessage());
}
有什么问题?来自用于改装的
onFailure(呼叫,可丢弃的t)
在与服务器对话时发生网络异常,或在创建请求或处理响应时发生意外异常时调用
如果您的特定请求在POSTMAN上没有问题,则不可能进行onFailure()
回调,除非:
List
POST/GET/PUT/。
)错误new-OkHttpClient.Builder().connectTimeout(100,TimeUnit.SECONDS)//连接超时
onFailure()
回调中执行以下操作,它就像一个符咒:
Log.e(TAG, t.getLocalizedMessage());
Log.e(TAG, t.getMessage());
t.printStackTrace();
检查您的url是否正常在浏览器上点击url并检查结果在postman上是否正常。检查您的pojo或JSON数组或objectTry使用t.getLocalizedMessage();它也是空的。当我将超时时间改为60秒时,它是正常的,但是非常慢。当我将超时时间设置为60秒时,它工作正常。所以问题不是1号,2号,3号和4号。t.getLocalizedMessage()和t.getMessage()都返回null,t.toString()返回java.net.SocketTimeoutException.@behrooz.fard哦,是的,这也是一种情况。如果网络连接低,默认超时时间为秒。我会在我的回答中加上这个。为android开发网络相关应用是一项棘手的工作,我们应该始终以低带宽使用率和低速连接为目标。
Log.e(TAG, t.getLocalizedMessage());
Log.e(TAG, t.getMessage());
t.printStackTrace();