This repository was archived by the owner on Feb 26, 2018. It is now read-only.
This repository was archived by the owner on Feb 26, 2018. It is now read-only.
v1.0.17 JSONObjectRequest and the debugger never hit the getParams method. #82
Closed
Description
This is request inner class extends from JsonObjectRequest and I override the getParams method to set custom POST params.
private class ImageUploadRequest extends JsonObjectRequest {
public ImageUploadRequest(int method, String url, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
super(method, url, listener, errorListener);
}
@Override
public Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("cover[base64]", imageThumbnailBase64Str);
params.put("cover[name]", null);
params.put("ref_g", "Wap");
params.put("ref_m", "Platform");
params.put("ref_a", "register");
params.put("access_token", Constants.ACCESS_TOKEN_UPLOAD_IMAGE);
Log.v(TAG, params.toString());
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> headers = new HashMap<String, String>();
headers.put("Content-Type","application/x-www-form-urlencoded");
headers.put("abc", "value");
return headers;
}
}
and this is the code post the request to server.
ImageUploadRequest request = new ImageUploadRequest(
1,
String.format("%s/index.php?g=Wap&m=Upload&a=server&token=%s&is_ngdata=1",
Constants.SERVER_URL, Constants.TOKEN),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// status: 0 失败,status: 1 成功
// error 错误消息
Log.v(TAG, response.toString());
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
}
});
WeiPingTaiApplication.getInstance().getRequestQueue().add(request);
but the getParams never been triggered.
Activity
mcxiaoke commentedon Aug 3, 2015
@chenghaojun JsonRequest is for using JSONObject as request body and response body, it ignores the params and uses body from mRequestBody you pass in the constructor, so you should extends Request and create custom Request subclass. see here: JsonRequest.java
chenghaojun commentedon Aug 3, 2015
Thanks.
I passed my params in the constructor, and the request posted to server as expected. but the request Content-Typs is application/json, my server can't resolve json data.
I override the getHeaders() method, change the Content-Type from application/json to application/x-www-form-urlencoded, the server correctly handle my request.
code:
