自定义的Request
package cn.flymeal.androidApp.http;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
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.google.gson.Gson;
/**
- 自定义网络请求(基于Volley)
- @author zx
-
@param
*/
public class GsonRequest extends Request {private static final String PROTOCOL_CHARSET = "utf-8";
private static final String PROTOCOL_CONTENT_TYPE = String.format(
"application/json; charset=%s", PROTOCOL_CHARSET);private Gson mGson;
private final Listener mListener;
private Map mParams=new HashMap(); ;
private Class mClass;
private Map headers = new HashMap();public GsonRequest(int method, String url, Class cls,
Map params, Map headers,
Listener listener, ErrorListener errorListener) {super(method, url, errorListener); mGson = new Gson(); mClass = cls; mListener = listener; mParams = params; this.headers = headers;
}
/**
-
将返回的数据处理
*/
@Override
protected Response parseNetworkResponse(NetworkResponse response) {
try {
String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
T parseJson = mGson.fromJson(jsonString, mClass);
return Response.success(parseJson,
HttpHeaderParser.parseCacheHeaders(response));} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
}
}
@Override
protected void deliverResponse(T response) {mListener.onResponse(response);
}
@Override
public Map getHeaders() throws AuthFailureError {return headers != null ? headers : super.getHeaders();
}
public void setHeaders(Map headers) {
this.headers = headers;
}
@Override
public String getBodyContentType() {return PROTOCOL_CONTENT_TYPE;
}
@Override
protected Map getParams() throws AuthFailureError {return mParams;
}
-
}
这是分装好的一个发送请求的方法
/**
* 基于Gson的网络Volley请求(新接口返回数据格式Volley请求)
*
* @param method
* @param url
* @param params
* @param requestListener
*/
public void executeGsonRequest(int method, String url,
Map params, Map headers,
final RequestListener requestListener) {
requestListener.onStart();
request = new GsonRequest<ResponseData>(method, url, ResponseData.class,
params, headers, new Listener<ResponseData>() {
@Override
public void onResponse(ResponseData response) {
if (response.getStatus().equals("success")) {
requestListener.onSuccess(response.getData());
Log.i("volley", response.toString());
} else {
requestListener.onMessage(response.getMsg());
}
closeDialog();
}
}, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
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) {
}
requestListener.onError(error);
closeDialog();
}
});
requestQueue.add(request);
}
public void checkNum(Context context,String str,RequestListener requestListener){
UrlParamJoin urlJoin=new UrlParamJoin("mvc/order/api/mobile/submitOrder.do");
Map<String, String> headers=new HashMap<String, String>();
headers.put(GlobalConstant.API_VERSION, "v3");
Map<String, String> param=new HashMap<String, String>();
param.put("orderStr", str);
FlymealHttpClient httpClient=new FlymealHttpClient(context);
httpClient.executeGsonRequest(Method.POST, urlJoin.getUrl(), param, headers, requestListener);
}
这是一个业务方法 求大神指导为什么POST请求参数没有传过去 ?在线等!!!1