Search code examples
androidandroid-volleywifiesp32

ESP32 Android App communication over HTTP


I have been trying to connect an ESP32 to my Android app using SoftAP and Webserver in ESP32 and Volley Library in android. I am able to connect to the ESP32 webserver from a web browser. But I am not able to access the webpage through android volley. I am getting a null error response in volley.

My phone is able to connect to the SoftAP.

Here is the ESP32 code of the server:

void setup()
{
  pinMode(pin_led, OUTPUT);
  Serial.begin(115200);
  SPIFFS.begin();

  wifiConnect();

  server.on("/l",[](){server.send_P(200,"text/html", webpage);});
  server.on("/toggle",toggleLED);
  server.on("/settings", HTTP_POST, handleSettingsUpdate);

  server.begin();
}

Here is my volley request:

RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
        String url ="http://192.168.4.1/l";

        // Request a string response from the provided URL.
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        // Display the first 500 characters of the response string.
                        Log.d("Connection", "Response: "+response);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                try{
                    Log.d("Connection", error.networkResponse.toString());
                } catch (Exception e){
                    Log.d("Connection", e.toString());
                }
            }
        });

// Add the request to the RequestQueue.
        stringRequest.setRetryPolicy(new DefaultRetryPolicy(
                10000,
                1,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        queue.add(stringRequest);

Solution

  • It seemed the issue was because "Cleartext Traffic" was not enabled by default in Oreo. Once I added the following code in the manifest it was working.

    <activity android:name=".MainActivity" android:usesCleartextTraffic="true">
    

    The solution was found in detail from this StackOverflow answer to a similar issue.