现在的位置: 首页 > 综合 > 正文

请教

2017年11月05日 ⁄ 综合 ⁄ 共 2487字 ⁄ 字号 评论关闭
/**
	 * Post 请求,返回值格式为String
	 * 
	 * @param url
	 * @param paramsForPost
	 * @param success
	 * @param error
	 */
	public static void getStringForPost(Context mContext, final String mTag,
			String url, HashMap<String, String> paramsForPost,
			final Listener<String> success, final ErrorListener error) {
		try {
			if (!isConnected(mContext)) {
				Toast.makeText(mContext, "网络不可用,请检查网络连接!!~", Toast.LENGTH_LONG)
						.show();
				return;
			}
			//向CustomStringRequest中传递向服务器上传的参数paramsForPost
                        CustomStringRequest sReq = new CustomStringRequest(Method.POST,url,
					paramsForPost,new Listener<String>() {
						@Override
						public void onResponse(String response) {
							success.onResponse(response);
							cancelRequest(mTag);
						}
					}, new ErrorListener() {
						@Override
						public void onErrorResponse(VolleyError e) {
							error.onErrorResponse(e);
							cancelRequest(mTag);
						}
					});
			getVolleyUtilsInstance().addToRequestQueue(sReq, mTag);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
package com.example.custom.volley;

import java.io.UnsupportedEncodingException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;

/**
 * 自定义Request,添加Cookie管理。返回值类型为String
 * 
 */
public class CustomStringRequest extends StringRequest {

	private HashMap<String, String> params=new HashMap<String, String>();
	//接收参数
	public CustomStringRequest(int method, String url,HashMap<String, String> param,
			Listener<String> listener, ErrorListener errorListener) {
		
		super(method, url,listener, errorListener);
		
		params=param;
	}
	
	public CustomStringRequest(int method, String url,
			Listener<String> listener, ErrorListener errorListener) {	
		super(method, url,listener, errorListener);
	}

	@Override
	protected Response<String> parseNetworkResponse(NetworkResponse response) {

		VolleyUtils.getInstance().checkSessionCookie(response.headers);

		String str = null;
		try {
			str = new String(response.data, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return Response.success(str,
				HttpHeaderParser.parseCacheHeaders(response));
	}

	@Override
	public Map<String, String> getHeaders() throws AuthFailureError {
		Map<String, String> headers = super.getHeaders();

		if (headers == null || headers.equals(Collections.emptyMap())) {
			headers = new HashMap<String, String>();
		}

		VolleyUtils.getInstance().addSessionCookie(headers);

		return headers;
	}
 
     //重写getParams()方法来传递参数	
     @Override
     protected HashMap<String, String> getParams() throws com.android.volley.AuthFailureError
     {
         return params;
     }

}

抱歉!评论已关闭.