Java 如何修复改装中的连接超时
我的连接超时:Java 如何修复改装中的连接超时,java,android,retrofit,Java,Android,Retrofit,我的连接超时: 2019-09-01 18:20:52.732 9389-9389/com.example.homecinema E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.homecinema, PID: 9389 io.reactivex.exceptions.OnErrorNotImplementedException: connect timed out at io.reactivex.internal
2019-09-01 18:20:52.732 9389-9389/com.example.homecinema E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.homecinema, PID: 9389
io.reactivex.exceptions.OnErrorNotImplementedException: connect timed out
at io.reactivex.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.java:704)
at io.reactivex.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.java:701)
at io.reactivex.internal.observers.LambdaObserver.onError(LambdaObserver.java:77)
at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.checkTerminated(ObservableObserveOn.java:276)
at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.drainNormal(ObservableObserveOn.java:172)
at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.run(ObservableObserveOn.java:252)
at io.reactivex.android.schedulers.HandlerScheduler$ScheduledRunnable.run(HandlerScheduler.java:109)
at...
代码概述如下
public void loginUser(String email, String password) {
compositeDisposable.add(myAPI.loginUser(email,password)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<String>() {
@Override
public void accept(String s) throws Exception {
if(s.contains("encrypted_password")){
Toast.makeText(getApplicationContext(), "login
Successful" , LENGTH_SHORT).show();
}
else {
Toast.makeText(getApplicationContext(),""+s,
LENGTH_SHORT).show();
}
}
})
);
public void登录用户(字符串电子邮件、字符串密码){
compositeDisposable.add(myAPI.logiuser)(电子邮件、密码)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.订阅(新消费者){
@凌驾
public void accept(字符串s)引发异常{
如果(s.包含(“加密密码”)){
Toast.makeText(getApplicationContext(),“登录
成功”,长度(短)。显示();
}
否则{
Toast.makeText(getApplicationContext(),“”+s,
长度(短)。显示();
}
}
})
);
在Retrotit Builder中使用自定义Http客户端,如下所示:
公共类配置{
公共改装getConfig(){
返回新的改装.Builder().client(httpClient())
.baseUrl(ApiDomain.BASE\u URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
私有OkHttpClient httpClient(){
返回新的OkHttpClient.Builder()
.connectTimeout(20,时间单位为秒)
.writeTimeout(20,时间单位。秒)
.readTimeout(30,时间单位为秒)
.build();
}
}
这将管理您的改装超时。当然,请确保在超时之前收到响应,否则您还需要处理重试操作。将自定义Http客户端与您的Retrotit Builder一起使用,如下所示:
公共类配置{
公共改装getConfig(){
返回新的改装.Builder().client(httpClient())
.baseUrl(ApiDomain.BASE\u URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
私有OkHttpClient httpClient(){
返回新的OkHttpClient.Builder()
.connectTimeout(20,时间单位为秒)
.writeTimeout(20,时间单位。秒)
.readTimeout(30,时间单位为秒)
.build();
}
}
这将管理您的改装超时。当然,请确保在超时之前收到响应,否则您还需要处理重试操作。您在哪里设置改装生成器?您可以在那里添加客户端。您在哪里设置改装生成器?您可以在那里添加客户端。