java - timeouterror - Volley RequestQueue Timeout
volley timeouterror android studio (5)
Algo como esto
RetryPolicy retryPolicy = new DefaultRetryPolicy(
YOUR_TIMEOUT_MS,
YOUT_MAX_RETRIES,
YOUR_BACKOFF_MULT
);
JsonObjectRequest request = new JsonObjectRequest(...);
request.setRetryPolicy(retryPolicy);
O podría implementar su propia RetryPolicy
.
RequestQueue mRequestQueue = Volley.newRequestQueue(getApplicationContext());
mRequestQueue.add(new JsonObjectRequest(Method.GET, cityListUrl, null, new Listener<JSONObject>()
{
public void onResponse(JSONObject jsonResults)
{
//Any Call
}
}, new ErrorListener()
{
public void onErrorResponse(VolleyError arg0)
{
//Any Error log
}
}
));
Esta es mi Solicitud de llamada y quiero cambiar o configurar el tiempo de espera para la solicitud. ¿Es posible de todos modos?
Debe establecer la RetryPolicy la solicitud:
myRequest.setRetryPolicy(new DefaultRetryPolicy(
MY_SOCKET_TIMEOUT_MS,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
Esto cambiaría tu código a:
RequestQueue mRequestQueue = Volley.newRequestQueue(getApplicationContext());
JsonObjectRequest request = new JsonObjectRequest(Method.GET, cityListUrl, null, new
Listener<JSONObject>() {
public void onResponse(JSONObject jsonResults) {
//Any Call
}
}, new ErrorListener(){
public void onErrorResponse(VolleyError arg0) {
//Any Error log
}
}
);
int socketTimeout = 30000;//30 seconds - change to what you want
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
request.setRetryPolicy(policy);
mRequestQueue.add(request);
Si recién está empezando con Volley , le droidQuery considerar droidQuery , que es un poco más fácil de configurar:
int socketTimeout = 30000;
$.ajax(new AjaxOptions().url(cityListUrl)
.timeout(socketTimeout)
.success(new Function() {
public void invoke($ d, Object... args) {
JSONObject jsonResults = (JSONObject) args[0];
//Any call
}
})
.error(new Function() {
public void invoke($ d, Object... args) {
AjaxError error = (AjaxError) args[0];
Log.e("Ajax", error.toString());
}
}));
Esto me funciona:
RequestQueue mRequestQueue = Volley.newRequestQueue(getApplicationContext());
JsonObjectRequest request = new JsonObjectRequest(Method.GET, cityListUrl, null, new
Listener<JSONObject>() {
public void onResponse(JSONObject jsonResults) {
//Any Call
}
}, new ErrorListener(){
public void onErrorResponse(VolleyError arg0) {
//Any Error log
}
}
);
int socketTimeout = 30000;//30 seconds - change to what you want
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
request.setRetryPolicy(policy);
mRequestQueue.add(request);
String url = "https://api.joind.in/v2.1/events?start=" + start + "&resultsperpage=20&format=json";
Log.i("DREG", "onLoadMoreItems: " + url);
final StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Add Code Here
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (error instanceof NetworkError) {
} else if (error instanceof ServerError) {
} else if (error instanceof AuthFailureError) {
} else if (error instanceof ParseError) {
} else if (error instanceof NoConnectionError) {
} else if (error instanceof TimeoutError) {
Toast.makeText(getContext(),
"Oops. Timeout error!",
Toast.LENGTH_LONG).show();
}
}
}
);
stringRequest.setRetryPolicy(new DefaultRetryPolicy(
10000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
requestQueue.add(stringRequest);
void RequestVolley() {
// Instantiate the RequestQuee
RequestQueue queue = Volley.newRequestQueue(getApplication());
//create new volley request
JsonObjectRequest requestNew = new JsonObjectRequest(Request.Method.GET, Url, null, createMyReqSuccessListener(), createMyReqErrorListener());
//Response.Listener and Error.Listener defined afterwards
//first param is TIMEOUT ...integer
//second param is number of retries ...integer
//third is backoff multiplier ...integer
requestNew.setRetryPolicy(new DefaultRetryPolicy(6000, 1, 1));
queue.add(requestNew);
}
private Response.Listener < JSONObject > createMyReqSuccessListener() {
return new Response.Listener < JSONObject > () {
@Override
public void onResponse(JSONObject response) {
//do something
}
};
}
private Response.ErrorListener createMyReqErrorListener() {
return new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//do something
}
};
}