Search code examples
androidkotlinhttprequestandroid-volleyngrok

Volley, ngrok - Can't make a post request using Volley to a ngrok REST api


I have been using ngrok for a bunch of projects, mostly voice apps and rest apis. I just needed to use my rest api in an android app. However, whatever I do, it does not work for some reasons!

If I try it with http, this is the error I get:

E/Volley: [17118] BasicNetwork.performRequest: Unexpected response code 307 for http://1z5d90b4.ngrok.io/api/v1/users/id/
I/System.out: That didn't work! com.android.volley.ServerError 

If I try the https link, this is what I get:


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

I don't know why and what is going on! I checked ngrok runnign on my computer, and it does not receive anything. FYI, I am making a post request on that url using Volley in Kotlin, this is my code:

        val queue = Volley.newRequestQueue(this)

    // Request a string response from the provided URL.
        val stringRequest = JsonObjectRequest(Request.Method.POST, apiUrl, params,
                Response.Listener { response ->
                    // Display the first 500 characters of the response string.
                    println("Response is: $response")
                },
                Response.ErrorListener { error ->  println("That didn't work! ${error}") })
        queue.add(stringRequest)


Solution

  • I found the problem! Sounds silly, but it was the network firewall! Once I switched the network, everything worked out!!!

    Just to add something, if you had an issue with HTTP request, try to add android:usesCleartextTraffic="true" to AndroidManifest.xml file. It will be something like this:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest ...>
    
        <!--Add this line if you haven't added it yet-->
        <uses-permission android:name="android.permission.INTERNET" />
    
        <application
            ...
    
           <!--ALSO, this can fix some of the problems-->
            android:usesCleartextTraffic="true"
    
    
            ...>
            ...
        </application>
    </manifest>