Skip to content

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

1. Purpose

In this post, I will demonstrate how to resolve the Connection reset error when using the Volley HTTP client to access the internet.

2. Environment

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

3. The Problem

When performing a simple HTTP POST using Volley, sometimes we encounter the following exception:

Terminal window
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)
Caused by: com.android.volley.NoConnectionError: java.net.SocketException: Connection reset
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:181)
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:215)
... 3 more
V/FA: Inactivity, disconnecting from the service

Here is the code used to connect to the internet and perform an HTTP POST:

VolleyHttpUtils.java
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

To resolve the issue, enable the mobile phone’s Wi-Fi hotspot and connect the computer to this hotspot. After doing so, the connection should work as expected.

5. Summary

In this post, I demonstrated how to resolve the Connection reset error when using Android Volley to connect to internet services. The key solution involves enabling the mobile phone’s Wi-Fi hotspot and connecting the computer to it. This approach ensures a stable network connection, which is essential for successful HTTP requests using Volley.

Final Words + More Resources

My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!