android-How to solve the 'Connection reset' error when using android volley to connect internet services?

1. Purpose

In this post, I would demo how to solve the connection reset error when using volley http client to access internet:

2. Environment

  • Android Studio 3.x
  • Last Update Date: 2021.8

3. The problem

When we do a simple http post using volley , sometimes, we get this exception:

E/xxx: add group failed dddd
    java.util.concurrent.ExecutionException: com.android.volley.NoConnectionError: java.net.SocketException: Connection reset
        at com.android.volley.toolbox.RequestFuture.doGet(RequestFuture.java:124)
        at com.android.volley.toolbox.RequestFuture.get(RequestFuture.java:97)
        at com.xxx.yyy.utils.VolleyHttpUtils.doPostStringSync(VolleyHttpUtils.java:140)
        at com.xxx.yyy.utils.JSONUtils.postJsonReturnString(JSONUtils.java:445)
        at com.xxx.yyy.ui.rule.zzz.doRealCreateNewGroupTask(EditFwdByNetGroupTargetsActivity.java:291)
        at com.xxx.yyy.ui.rule.zzz.access$400(EditFwdByNetGroupTargetsActivity.java:84)
        at com.xxx.yyy.ui.rule.zzz$6.subscribe(EditFwdByNetGroupTargetsActivity.java:254)
        at io.reactivex.internal.operators.observable.ObservableCreate.subscribeActual(ObservableCreate.java:40)
        at io.reactivex.Observable.subscribe(Observable.java:12030)
        at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeTask.run(ObservableSubscribeOn.java:96)
        at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:579)
        at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66)
        at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:923)
     Caused by: com.android.volley.NoConnectionError: java.net.SocketException: Connection reset
        at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:181)
        at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:131)
        at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:111)
        at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:90)
     Caused by: java.net.SocketException: Connection reset
        at java.net.SocketInputStream.read(SocketInputStream.java:215)
        at java.net.SocketInputStream.read(SocketInputStream.java:144)
        at com.android.org.conscrypt.ConscryptEngineSocket$SSLInputStream.readFromSocket(ConscryptEngineSocket.java:936)
        at com.android.org.conscrypt.ConscryptEngineSocket$SSLInputStream.processDataFromSocket(ConscryptEngineSocket.java:900)
        at com.android.org.conscrypt.ConscryptEngineSocket$SSLInputStream.access$100(ConscryptEngineSocket.java:722)
        at com.android.org.conscrypt.ConscryptEngineSocket.doHandshake(ConscryptEngineSocket.java:238)
        at com.android.org.conscrypt.ConscryptEngineSocket.startHandshake(ConscryptEngineSocket.java:217)
        at com.android.okhttp.internal.io.RealConnection.connectTls(RealConnection.java:196)
        at com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:153)
        at com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:116)
        at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:186)
        at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:128)
        at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
        at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:289)
        at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:232)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:465)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:131)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:262)
        at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:219)
        at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:30)
        at com.android.volley.toolbox.HurlStack.addBody(HurlStack.java:292)
        at com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:277)
        at com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:249)
        at com.android.volley.toolbox.HurlStack.executeRequest(HurlStack.java:94)
        at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:123)
        	... 3 more
V/FA: Inactivity, disconnecting from the service


Here is the code that we used to connect internet and do HTTP post:

 public static String doPostStringSync(Context context, final String url, String body) throws Exception {
        RequestFuture<String> requestFuture=RequestFuture.newFuture();
        StringRequest postRequest = new MyStringRequest(Request.Method.POST,
                url,requestFuture,requestFuture) {
            @Override
            public Map<String, String> getHeaders() {
                HashMap<String, String> headers = new HashMap<String, String>();
                headers.put("Accept", "application/json");
                headers.put("Content-Type", "application/json; charset=UTF-8");
                return headers;
            }
            @Override
            public byte[] getBody() throws AuthFailureError {
                try {
                    return body.getBytes("utf-8");
                } catch (UnsupportedEncodingException e) {
                    LogUtils.error("",e);
                }
                return null;
            }

            @Override
            public String getBodyContentType() {
                return "application/json; charset=utf-8";
            }
        };
        VolleySingleton.getInstance(context).addToRequestQueue(postRequest);
        return requestFuture.get(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
    }

4. The Solution

Turn on the mobile phone’s wifi hotspot, let the computer connect to this hotspot. Then test again. It works!

5. Summary

In this post, I demonstrated how to fix the internet-connection-problem when using android volley.

-->