I want to make a web service that allow my app to connect with a database in localhost. I'm trying to follow this example making some changes in the names.
In my android code I have this:
VolleySingleton.
getInstance(getApplicationContext()).
addToRequestQueue(new JsonObjectRequest(Request.Method.GET, Constantes.GET, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
String result = response.toString();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (error.networkResponse == null) {
if (error.getClass().equals(TimeoutError.class)) {
// Show timeout error message
Log.e("error volley",error.getStackTrace().toString());
Toast.makeText(getApplicationContext(),
"Oops. Timeout error!",
Toast.LENGTH_LONG).show();
}
}
}
}
));
VolleySingleton class:
public final class VolleySingleton {
// Atributos
private static VolleySingleton singleton;
private RequestQueue requestQueue;
private static Context context;
private VolleySingleton(Context context) {
VolleySingleton.context = context;
requestQueue = getRequestQueue();
}
public static synchronized VolleySingleton getInstance(Context context) {
if (singleton == null) {
singleton = new VolleySingleton(context.getApplicationContext());
}
return singleton;
}
public RequestQueue getRequestQueue() {
if (requestQueue == null) {
requestQueue = Volley.newRequestQueue(context.getApplicationContext());
}
return requestQueue;
}
public <T> void addToRequestQueue(Request<T> req) {
getRequestQueue().add(req);
}
The constants that I have in Constantes class:
private static final String PUERTO_HOST = "63343";
private static final String IP = "http://10.0.2.2:";
public static final String GET = IP + PUERTO_HOST + "/prueba/obtener_canales.php";
I used that ip because I read that 10.0.2.2 is for android studio emulator and 10.0.3.2 is for genymotion.
I'm using XAMPP and the route of my file is "D:\xampp\htdocs\prueba\obtener_canales.php
I'm getting all time the Toast with timeout error and when I debug in android studio there's nothing to see.