gpt4 book ai didi

android - 改造中的 SocketTimeoutException

转载 作者:IT老高 更新时间:2023-10-28 21:45:28 25 4
gpt4 key购买 nike

我正在尝试向服务器发送 POST 请求以获取数据,但有时会发生 SocketTimeoutException!

我使用 Ok3Client 解决了它,但我遇到了同样的异常我该如何解决它?

我的代码在下面

public void getNormalLogin() {
if (mProgressDialog == null) {
mProgressDialog = ViewUtils.createProgressDialog(mActivity);
mProgressDialog.show();
} else {
mProgressDialog.show();
}

if (Build.VERSION.SDK != null && Build.VERSION.SDK_INT > 13) {
restadapter = new RestAdapter.Builder().setEndpoint(HOST).setLogLevel(RestAdapter.LogLevel.FULL).setClient(new Ok3Client(new OkHttpClient())).build();
mApi = restadapter.create(Api.class);
mApi.SignIn(etEmail.getText().toString(), etPassword.getText().toString(), new Callback<ArrayList<SignUpMainBean>>() {
@Override
public void success(ArrayList<SignUpMainBean> signUpMainBeen, Response response) {
mProgressDialog.dismiss();
LOGD("Status:: ::", String.valueOf(response.getStatus()));
LOGD("Code:: ::", String.valueOf(signUpMainBeen.get(0).getCode()));
if (signUpMainBeen != null && signUpMainBeen.size() > 0) {
if (signUpMainBeen.get(0).getCode() == 1) {

for (int i = 0; i < signUpMainBeen.size(); i++) {
for (int j = 0; j < signUpMainBeen.get(i).getResult().size(); j++) {
showToast(mActivity, getString(R.string.you_have_successfully_login), Toast.LENGTH_SHORT);
LOGD("Success", "Success");
finish();
SignIn.mActivity.finish();
}
}

} else if (signUpMainBeen.get(0).getCode() == 0) {
mProgressDialog.dismiss();
showToast(mActivity, getString(R.string.invalid_email_password), Toast.LENGTH_SHORT);
LOGD("Invalid email or password", "Invalid email or password");
} else if (signUpMainBeen.get(0).getCode() == -1) {
mProgressDialog.dismiss();
showToast(mActivity, getString(R.string.your_account_is_inactive), Toast.LENGTH_SHORT);
LOGD("Your account is inactive", "Your account is inactive");
}
}
}

@Override
public void failure(RetrofitError error) {
mProgressDialog.dismiss();
showToast(mActivity, getString(R.string.can_not_connect_to_server), Toast.LENGTH_SHORT);
LOGD("Failure", "Failure");
}
});
}
}

我的错误日志是

D/Retrofit: java.net.SocketTimeoutException
at java.net.PlainSocketImpl.read(PlainSocketImpl.java:532)
at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:40)
at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:255)
at okio.Okio$2.read(Okio.java:139)
at okio.AsyncTimeout$2.read(AsyncTimeout.java:211)
at okio.RealBufferedSource.indexOf(RealBufferedSource.java:306)
at okio.RealBufferedSource.indexOf(RealBufferedSource.java:300)
at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:196)
at okhttp3.internal.http.Http1xStream.readResponse(Http1xStream.java:185)
at okhttp3.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:126)
at okhttp3.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:723)
at okhttp3.internal.http.HttpEngine.access$200(HttpEngine.java:81)
at okhttp3.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:708)
at okhttp3.internal.http.HttpEngine.readResponse(HttpEngine.java:563)
at okhttp3.RealCall.getResponse(RealCall.java:241)
at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:198)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:160)
at okhttp3.RealCall.execute(RealCall.java:57)
at com.jakewharton.retrofit.Ok3Client.execute(Ok3Client.java:40)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:326)
at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)
at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:278)
at retrofit.CallbackRunnable.run(CallbackRunnable.java:42)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at retrofit.Platform$Android$2$1.run(Platform.java:142)
at java.lang.Thread.run(Thread.java:818)

最佳答案

如果提取需要更多时间,请增加时间使用此代码对我有用

 OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(100, TimeUnit.SECONDS)
.readTimeout(100,TimeUnit.SECONDS).build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("yourbaseurl").client(client)
.addConverterFactory(GsonConverterFactory.create(new Gson())).build();

从评论开始,这是一种解决方法,而不是最佳实践, 后端不应花费这么长时间,并且必须通知用户错误或重试消息

关于android - 改造中的 SocketTimeoutException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39219094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com