Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
251 views
in Technique[技术] by (71.8m points)

android - SocketTimeoutException in Retrofit

I am trying to POST request to server for fetch data but sometime It's occure SocketTimeoutException!

I used Ok3Client to resolve it but I facing the same Exception How can I resolve it?

My code is below

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");
                }
            });
        }
    }

My Error Log is

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Increase the time if the fetching is taking more time use this code it worked for me

 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();

hay this is a work around not the best practice , from comments down, Back end should not take this long and user must be notified with error or try again message


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...