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!