0
votes

I wrote android app to read json using volley.

If i use example URL from internet - i don't have problem, the data is read. But when i try to read data from local host, I am getting an error. The same data i can see in web browser in my emulator. my URL: "http://10.0.2.2/volley_sample/get_data.php"

my code:

     private void sendGetRequest() {

        RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
       
        String url = "http://10.0.2.2/volley_sample/get_data.php"; //it doesn't work

        //String url ="https://reqres.in/api/users/2"; //it works

      // 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 10 characters of the response string.
                        get_response_text.setText("Response is: " + response.substring(0, 10));
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                get_response_text.setText("That didn't work!");
            }
        });

// Add the request to the RequestQueue.
        queue.add(stringRequest);
    }

adres "http://10.0.2.2/volley_sample/get_data.php" works in web browser in my emulator. I tried also "http://192.168....", "127.0.0.1....", "localhost/volley_sample...." etc., and neither works.

adres "https://reqres.in/api/users/2" (example from internet) work without problem. ofc. i added "<uses-permission android:name="android.permission.INTERNET"/>"

I have no idea what I am doing wrong :(

Allowed clear text traffic?blackapps
If i use example URL from internet http or https?blackapps
from internet is https. Local is httpKrzysztof Marchewka
Well you could have said more now. Try an internet url with http to see that it will not go. And why didnt you react on my first comment?blackapps
my script to read: <?php $data=array("data_1"=>"Value 1","data_2"=>"Value 2","data_3"=>"Value 3"); echo json_encode($data);Krzysztof Marchewka