我正在列表视图中填写医生列表并且工作正常。如果没有在30秒内从服务器获得响应,现在我想要打开警报。我如何使用下面的代码中的截击来做到这一点。我已经检查了互联网连接,但我也想查看回复。我怎样才能做到这一点?
public void getDoctorlist(final String email, final String sessionid) {
String tag_string_req = "req_service";
pDialog.setMessage("Getting Details");
showDialog();
StringRequest strReq = new StringRequest(Method.POST, AppConfig.URL_Doc_list, new Response.Listener<String>() {
@Override
public void onResponse(String inventoryList) {
Log.d("++++++", inventoryList.toString());
hideDialog();
try {
JSONObject jObj = new JSONObject(inventoryList);
String Errorcode = jObj.getString("responseCode");
if (Errorcode.equalsIgnoreCase("101")) {
Toast.makeText(getApplicationContext(), "Please Try again later", Toast.LENGTH_SHORT).show();
}
if (Errorcode.equalsIgnoreCase("102")) {
JSONArray json_user = jObj.getJSONArray("dataList");
for (int i = 0; i < json_user.length(); i++) {
try {
JSONObject obj = json_user.getJSONObject(i);
GetSetPaln doctor = new GetSetPaln(obj.getString("companyName"), obj.getString("clientId"),
obj.getString("clientIdentifier"), obj.getString("lastVisit"),
obj.getString("imageUrl"), obj.getString("emailId"), obj.getString("contactNo"),
obj.getString("address"), obj.getString("clientLocationId"),
obj.getString("city"));
doc_list.add(doctor);
} catch (JSONException e) {
e.printStackTrace();
}
}
ad_doctor = new DoctorAdapter(DoctorList.this, doc_list);
listview.setAdapter(ad_doctor);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
hideDialog();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("emailId", email);
params.put("sessionId", sessionid);
return params;
}
};
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
答案 0 :(得分:1)
gson_request.setRetryPolicy((new DefaultRetryPolicy(
20000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)));
you can handle timeout error OnErrorResponse:
public class GsonRequest<T> extends Request<T>
{
private final Gson gson = new Gson();
private final Class<T> clazz;
private final Response.Listener<T> listener;
private String parameters = null;
private VolleyErrorHandling volleyErrorHandling=new VolleyErrorHandling();
public GsonRequest(int post, String url, Class<T> clazz, String params, Response.Listener<T> listener, Response.ErrorListener errorListener) {
super(post, url, errorListener);
this.clazz = clazz;
this.listener = listener;
this.parameters = params;
}
@Override
protected Response<T> parseNetworkResponse(NetworkResponse networkResponse) {
try {
String json = new String(
networkResponse.data,
HttpHeaderParser.parseCharset(networkResponse.headers));
return Response.success(
gson.fromJson(json, clazz),
HttpHeaderParser.parseCacheHeaders(networkResponse));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JsonSyntaxException e) {
return Response.error(new ParseError(e));
}
}
@Override
protected void deliverResponse(T t) {
listener.onResponse(t);
}
@Override
public String getBodyContentType() {
return "application/json";
}
@Override
public byte[] getBody() throws AuthFailureError {
return parameters.toString().getBytes();
}
@Override
protected VolleyError parseNetworkError(VolleyError volleyError) {
NetworkResponse response = volleyError.networkResponse;
VolleyError error = null;
String strerrormsg = "";
if (response != null && response.data != null)
{
switch (response.statusCode)
{
case 400:
if (volleyError instanceof ServerError)
{
volleyError = new VolleyError(new String(volleyError.networkResponse.data));
if (volleyErrorHandling.is_empty(volleyError.getLocalizedMessage())) {
strerrormsg = "SERVER_ERROR";
error = new VolleyError(strerrormsg);
volleyError = error;
}
} else {
strerrormsg = "SERVER_ERROR";
error = new VolleyError(strerrormsg);
volleyError = error;
}
break;
case 401:
strerrormsg = "Bad Request";
error = new VolleyError(strerrormsg);
volleyError = error;
break;
case 403:
strerrormsg = "Unauthorized request to server";
error = new VolleyError(strerrormsg);
volleyError = error;
break;
case 404:
strerrormsg = "Something went wrong. Please try again later.";
error = new VolleyError(strerrormsg);
volleyError = error;
break;
case 408:
strerrormsg = "Something went wrong. Please try again later.";
error = new VolleyError(strerrormsg);
volleyError = error;
break;
case 500:
strerrormsg = "Oops something went wrong. Please try again later.";
error = new VolleyError(strerrormsg);
volleyError = error;
break;
case 503:
strerrormsg = "Oops something went wrong. Please try again later.";
error = new VolleyError(strerrormsg);
volleyError = error;
break;
default:
strerrormsg = "Oops something went wrong. Please try again later.";
error = new VolleyError(strerrormsg);
volleyError = error;
break;
}
}
else if (response == null)
{
if (volleyError.getClass().equals(TimeoutError.class)) {
strerrormsg = "Oops. Network connection timed out";
error = new VolleyError(strerrormsg);
volleyError = error;
}
}
return volleyError;
}
}
答案 1 :(得分:0)
这已经被问到了。 请阅读Change Volley timeout duration如何为Volley请求设置固定超时。
之后你应该使用onErrorResponse
来监听错误和超时
答案 2 :(得分:0)
通过Volley
RetryPolicy
,您可以超时30秒。
strReq.setRetryPolicy(new DefaultRetryPolicy(
MY_SOCKET_TIMEOUT_MS,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
在此节目中显示错误消息。
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Show dialog to show the error message from the server.
}
}
希望这有帮助。快乐的编码。
答案 3 :(得分:0)
您可以在RetryPolicy
strReq.setRetryPolicy(new DefaultRetryPolicy(
30000, //for 30 Seconds
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
然后检查您的onErrorResponse()
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
hideDialog();
}
}
希望它对你有所帮助。
答案 4 :(得分:0)
private void getResponce() {
RequestQueue requestQueue = Volley.newRequestQueue(this);
JsonObjectRequest jsObjRequest = new JsonObjectRequest
(Request.Method.GET, _url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
int socketTimeout = 30000; // 30 seconds. You can change it
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
jsObjRequest.setRetryPolicy(policy);
requestQueue.add(jsObjRequest);
}