简体   繁体   中英

Volley no connection error while trying to get a JSON object from a RESTful API

I am trying to fetch the user information about user ID 1 from a api and log it, however I keep running into the following error:

E/Rest response: com.android.volley.NoConnectionError: javax.net.ssl.SSLHandshakeException: SSL handshake aborted: ssl=0x7c74f2dc88: I/O error during system call, Connection reset by peer

This is my first time working with an RESTful API so I am a bit inexperienced. I have tried to find solutions to this problem on stack overflow but they do not seem to work. I have seen that some users mention that this is a commonly occurring error for Android devices below Android API 22 5.1 Lollipop, however I am using Android 6.0 Marshmallow and am still receiving this error.

This is my code:

public class MainActivity extends AppCompatActivity {

String url = "https://ecoproduce.eu/api/User/1";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

        final SSLSocketFactory sslSocketFactory;
        try {
            sslSocketFactory = new TLSSocketFactory();
            HttpsURLConnection.setDefaultSSLSocketFactory(sslSocketFactory);
        } catch (KeyManagementException ignored) {

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

    RequestQueue requestQueue = Volley.newRequestQueue(this);

    JsonObjectRequest objectRequest = new JsonObjectRequest(
            Request.Method.GET,
            url,
            null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.e("Rest response", response.toString());
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("Rest response", error.toString());
                }
            }
    );

    requestQueue.add(objectRequest);
}

}

Solved by switching the url string from

String url = "https://ecoproduce.eu/api/User/1";

to

String url = "http://ecoproduce.eu/api/User/1";

and adding

android:usesCleartextTraffic="true"

in the android manifest file. Answer found on the following link: Android 8: Cleartext HTTP traffic not permitted

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM