Java Code Examples for com.android.volley.toolbox.Volley#newNoCacheRequestQueue()

The following examples show how to use com.android.volley.toolbox.Volley#newNoCacheRequestQueue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpTools.java    From volley with Apache License 2.0 5 votes vote down vote up
/**
 * 发送http请求
 * @param request
 */
public <T> void sendRequest(Request<T> request) {
	if (sRequestQueue == null) {
        sRequestQueue = Volley.newNoCacheRequestQueue(mContext);
    }
	request.setTag(this);
    sRequestQueue.add(request);
}
 
Example 2
Source File: HttpTools.java    From volley with Apache License 2.0 4 votes vote down vote up
public static void init(Context context) {
    if (sRequestQueue == null) {
        sRequestQueue = Volley.newNoCacheRequestQueue(context.getApplicationContext());
    }
}
 
Example 3
Source File: HttpTools.java    From volley with Apache License 2.0 4 votes vote down vote up
public DownloadRequest download(final RequestInfo requestInfo, String target, final boolean isResume, final HttpCallback httpResult) {
	final String url = requestInfo.getFullUrl();
	VolleyLog.d("download->%s", url);
    DownloadRequest request = new DownloadRequest(url, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            if (httpResult != null) {
                httpResult.onResult(response);
                httpResult.onFinish();
            }
        }
    }, new Response.ErrorListener(){

        @Override
        public void onErrorResponse(VolleyError error) {
            if (httpResult != null) {
                httpResult.onError(error);
                httpResult.onFinish();
            }
        }
        
    }, new Response.LoadingListener() {

        @Override
        public void onLoading(long count, long current) {
            if (httpResult != null) {
                httpResult.onLoading(count, current);
            }
        }
    }) {
        @Override
        public void stopDownload() {
            super.stopDownload();
            if (httpResult != null) {
                httpResult.onCancelled();
            }
        }
        
        @Override
        public void cancel() {
            super.cancel();
            if (httpResult != null) {
                httpResult.onCancelled();
            }
        }
        
        
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
        	Map<String, String> headers = super.getHeaders();
        	if (headers != null) {
        		headers.putAll(requestInfo.getHeaders());
        	} else {
        		headers = requestInfo.getHeaders();
        	}
            return headers;
        }
    };
    request.setResume(isResume);
    request.setTarget(target);
    if (httpResult != null) {
        httpResult.onStart();
    }
    if (TextUtils.isEmpty(url)) {
        if (httpResult != null) {
            httpResult.onError(new Exception("url can not be empty!"));
            httpResult.onFinish();
        }
        return request;
    }
    if (sDownloadQueue == null) {
        sDownloadQueue = Volley.newNoCacheRequestQueue(mContext);
    }
    sDownloadQueue.add(request);
    return request;

}
 
Example 4
Source File: HttpTools.java    From volley with Apache License 2.0 4 votes vote down vote up
private void sendRequest(final int method, final RequestInfo requestInfo, final HttpCallback httpResult) {
    if (sRequestQueue == null) {
        sRequestQueue = Volley.newNoCacheRequestQueue(mContext);
    }
    if (httpResult != null) {
        httpResult.onStart();
    }
    if (requestInfo == null || TextUtils.isEmpty(requestInfo.url)) {
        if (httpResult != null) {
            httpResult.onError(new Exception("url can not be empty!"));
            httpResult.onFinish();
        }
        return;
    }
    switch (method) {
    case Request.Method.GET:
        requestInfo.url = requestInfo.getFullUrl();
        VolleyLog.d("get->%s", requestInfo.getUrl());
        break;
    case Request.Method.DELETE:
        requestInfo.url = requestInfo.getFullUrl();
        VolleyLog.d("delete->%s", requestInfo.getUrl());
        break;

    default:
        break;
    }
    final StringRequest request = new StringRequest(method, requestInfo.url, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            if (httpResult != null) {
                httpResult.onResult(response);
                httpResult.onFinish();
            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) { 
            if (httpResult != null) {
                httpResult.onError(error);
                httpResult.onFinish();
            }
        }
    }, new Response.LoadingListener() {

        @Override
        public void onLoading(long count, long current) {
            if (httpResult != null) {
                httpResult.onLoading(count, current);
            }
        }
    }) {

        @Override
        public void cancel() {
            super.cancel();
            if (httpResult != null) {
                httpResult.onCancelled();
            }
        }
        
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            if (method == Request.Method.POST || method == Request.Method.PUT) {
                VolleyLog.d((method == Request.Method.POST ? "post->%s" : "put->%s"), requestInfo.getUrl() + ",params->" + requestInfo.getParams().toString());
                return requestInfo.getParams();
            } 
            return super.getParams();
        }
        
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            return requestInfo.getHeaders();
        }
    };
    request.setTag(this);
    sRequestQueue.add(request);
}