Android凌空最后请求在网络上发帖请求?

时间:2014-05-07 04:05:59

标签: android android-volley

当有网络时,Volley效果很好,但是当网络掉线时,它很奇怪。

当网络使用兴奋剂时,即网络强度下降并且我发布了一个帖子请求然后没有任何反应但是一旦网络恢复到完全强度,它就会调用之前的帖子请求而不是新的请求?

当我清除缓存时效果很好......

如何解决这个问题,为什么我收到以前的帖子回复?

// Implements required listeners
public class VolleyServices<T> implements Response.Listener<T>, ErrorListener{

private int id; // request id
private RequestQueue request; // volley request queue
private VolleyResponce<T> listener; // callback listener
private Class<T> clazz;  // parsing class

public interface VolleyResponce<T> {
    public void OnSuccess(T response,int id);
    public void OnError(ServerError error,int id);
}

// Constructor
public VolleyServices(VolleyResponce<T> listener,Context context) {
    this.listener = listener;
    request = VolleyRequest.getInstance(context).getRequestQueue();
}

// call to post the request with parse class, post parameters in map and request id
public void post(String methodName, HashMap<String, String> map,Class<T> clazz,int id) {
    String url = baseUrl + methodName;
    this.clazz = clazz;
    this.id = id;
    GsonRequest<T> myReq = new GsonRequest<T>(Request.Method.POST,url,clazz,map,this,this);
    myReq.setTag(id);
    request.add(myReq);
}

@Override
public void onResponse(T response) {
    if(response != null && clazz == response.getClass()){               
        listener.OnSuccess(response,this.id);                               
    }       
}

@Override
public void onErrorResponse(VolleyError volleyError) {

    ServerError error = null;

    if(volleyError instanceof NetworkError) {
        error = new ServerError("No internet Access, Check your internet connection.","400");
    }

    if(volleyError instanceof AuthFailureError) {
        error = new ServerError("Authentication Failure","400");
    }

    if(volleyError instanceof ParseError) {
        error = new ServerError("Parsing error","400");
    }

    if(volleyError instanceof NoConnectionError) {
        error = new ServerError("No internet Access, Check your internet connection.","400");
    }

    if(volleyError instanceof TimeoutError) {
        error = new ServerError("Request timed out, Please try again later.","400");
    }

    if(volleyError.networkResponse != null && volleyError.networkResponse.statusCode == 500) {
        error = new ServerError("Internal server error","400");
    }

    else {
        try {
            if(volleyError.networkResponse != null) {
                String responseBody = new String(volleyError.networkResponse.data, "utf-8" );
                try{
                    Gson gson = new Gson();         
                    error = gson.fromJson(responseBody, ServerError.class);
                    if(error.status == null) {
                        error.status = "";
                    }
                }catch(Exception e){
                    e.printStackTrace();
                }
            } else {
                error = new ServerError("Unknown Error","400");
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
    listener.OnError(error,this.id);
}

我捕获了一些日志消息,详细解释了所有内容 log link

0 个答案:

没有答案