0

I have an LG G3 and when I run my app on it, Volley fails to work. All I am doing is sending a post request to my local server and it works perfectly on the emulator but then fails on the device. The error I get is the timeout error. Here is the code:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Button button = (Button) findViewById(R.id.button);
        final TextView textview = (TextView) findViewById((R.id.test));
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendRequest("example", "0", "0", "0", "0", "0", "0");

            }
        });
    }


    public void sendRequest(final String location, final String mood, final String gender, final String age,
                            final String day, final String month, final String year) {

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        String url = "http://10.0.2.2:8000/polls/add/";

        StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Toast.makeText(MainActivity.this, "works", Toast.LENGTH_SHORT).show();
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(MainActivity.this, "Error is: " + error.toString(),
                        Toast.LENGTH_LONG).show();
                Log.e("VOLLEY", error.toString());
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> parameters = new HashMap<>();
                parameters.put("age", age);
                parameters.put("location", location);
                parameters.put("mood", mood);
                parameters.put("gender", gender);
                parameters.put("day", day);
                parameters.put("month", month);
                parameters.put("year", year);
                return parameters;
            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("Content-Type", "application/x-www-form-urlencoded");
                return params;
            }
        };
        requestQueue.add(stringRequest);
    }
}  

Any help would be really appreciated. Thanks!

Bryan
  • 14,756
  • 10
  • 70
  • 125
Claus Lens
  • 119
  • 1
  • 4
  • Volley does not provide local access – user6313669 Apr 21 '17 at 19:55
  • Possible duplicate of [Accessing localhost of PC from USB connected Android mobile device](http://stackoverflow.com/questions/9887621/accessing-localhost-of-pc-from-usb-connected-android-mobile-device) – Bryan Apr 21 '17 at 19:56
  • See http://stackoverflow.com/questions/4779963/how-can-i-access-my-localhost-from-my-android-device – user6313669 Apr 21 '17 at 20:05

1 Answers1

1

if you want work with a local server you should use your in-network IP .... for example 192.168.1.105 instead of local IP address ( 10.0.2.2 - 127.0.0.1 - etc )

so ... just find you PC's IP address using ipconfig/all and then :

String url = "http://192.168.1.105:8000/polls/add/";
Arash Hatami
  • 5,297
  • 5
  • 39
  • 59
  • Thanks for the answer, but I now get a new error: com.android.volley.NoConnectionError: java.net.ConnectException: failed to connect to /192.168.x.x (port 8000) after 5000ms: isConnected failed: EHOSTUNREACH (No route to host) – Claus Lens Apr 22 '17 at 01:07
  • check your virtual server ( xammp / wamp ) ..... can you access via local browser ? – Arash Hatami Apr 23 '17 at 10:32
  • 1
    I found the error, thank you! The problem was that I was trying to connect to port 8000 that has a firewall. So all I had to do was change the port to another one and it worked. – Claus Lens Apr 26 '17 at 16:57