Google Volley 框架 POST请求无效 服务器接收不到参数
2014-07-07 13:04
453 查看
原帖地址:http://stackoverflow.com/questions/19837820/volley-jsonobjectrequest-post-request-not-working
做项目过程中,用了volley框架,其中POST请求,重写getParams()方法,竟然无效,加上Google被禁,弄了好久才找到原因:
this is not a bug, the getParams() doesn't invoke because JsonObjectRequest extended JsonRequest which
invoke getBody() directly to encoding the constructor second parameter(call requestBody) as contentType, that's why it ignore your getParam() method;
把修改过的类贴出来:
做项目过程中,用了volley框架,其中POST请求,重写getParams()方法,竟然无效,加上Google被禁,弄了好久才找到原因:
this is not a bug, the getParams() doesn't invoke because JsonObjectRequest extended JsonRequest which
invoke getBody() directly to encoding the constructor second parameter(call requestBody) as contentType, that's why it ignore your getParam() method;
把修改过的类贴出来:
import java.io.UnsupportedEncodingException; import java.util.Map; import org.json.JSONException; import org.json.JSONObject; 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; public class CustomRequest extends Request<JSONObject> { private Listener<JSONObject> listener; private Map<String, String> params; public CustomRequest(String url, Map<String, String> params, Listener<JSONObject> reponseListener, ErrorListener errorListener) { super(Method.GET, url, errorListener); this.listener = reponseListener; this.params = params; } public CustomRequest(int method, String url, Map<String, String> params, Listener<JSONObject> reponseListener, ErrorListener errorListener) { super(method, url, errorListener); this.listener = reponseListener; this.params = params; } protected Map<String, String> getParams() throws com.android.volley.AuthFailureError { return params; }; @Override protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) { try { String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); return Response.success(new JSONObject(jsonString), HttpHeaderParser.parseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } catch (JSONException je) { return Response.error(new ParseError(je)); } } @Override protected void deliverResponse(JSONObject response) { // TODO Auto-generated method stub listener.onResponse(response); } }
相关文章推荐
- 【小程序】POST请求服务器接收不到参数
- 微信小程序 post请求服务器接收不到参数
- wx.request的POST方法的参数传输服务器接收不到的bug。
- AngularJs中$http发送post或者get请求,SpringMVC后台接收不到参数值的解决办法
- angular的post请求,SpringMVC后台接收不到参数值的解决办法(参考加自创)
- Android客户端请求服务器的WebService时,服务器接收不到参数。
- angular的post请求,SpringMVC后台接收不到参数值的解决方案
- 解决angular的post请求后SpringMVC后台接收不到参数值问题的方法
- axios发送post请求springMVC接收不到参数的解决方法
- volley框架下,使用post方式请求服务器,传递参数和头
- NodeMCU教程 http.post请求及服务端接收不到参数解决方案
- AngularJs中$http发送post或者get请求,SpringMVC后台接收不到参数值的解决办法
- AFN利用cookie POST JSON服务器接收不到参数
- angularJS post请求后台接收不到参数
- Yaws(erlang web服务器框架) 学习记录之获取GET/POST请求参数
- ajax的post请求后台request.getParameter()接收不到参数问题
- 解决angular的post请求后SpringMVC后台接收不到参数值问题的方法
- axios发送post请求springMVC接收不到参数
- 小程序wx.request的POST方法的参数传输服务器接收不到
- angular的post请求,springmvc后台接收不到参数的解决方案