自定義的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;
/**
@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
有些版本的volley只支持StringRequest,然後參數以getParams形式傳入,提交方式為GET,樓主試一下吧,應該可以