【发布时间】:2015-10-09 12:38:29
【问题描述】:
我正在尝试使用 gson + volley 发出 PUT 请求,我必须在同一个请求中同时发送 Header 和 Params。我收到了成功响应,但没有将数据发送到服务器。我正在使用以下代码
public GsonRequest(int method,
String url,
Class<T> clazz,
Map<String, String> headers,
Map<String, String> params,
Listener<T> listener,
ErrorListener errorListener) {
super(method, url, errorListener);
this.clazz = clazz;
this.params = params;
this.listener = listener;
this.headers = headers;
mGson = new Gson();
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
return params != null ? params : super.getParams();
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
return headers != null ? headers : super.getHeaders();
}
在 Activity 中我使用上面的 gson
private void updateProfile(String fname, String lname, String email, String mob) {
String tag_json_obj = "json_obj_req";
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Authorization", "JWT " + tinyDB.getString(Constants.MY_SHARED_PREF_TOKEN));
Log.d("Authorization---", "JWT " + tinyDB.getString(Constants.MY_SHARED_PREF_TOKEN));
String url = Constants.PATCH_USER+tinyDB.getString(Constants.MY_USER_ID);
Map<String, String> params = new HashMap<String, String>();
params.put("Content-type", "application/x-www-form-urlencoded");
params.put("first_name",fname);
params.put("last_name",lname);
params.put("email",email);
params.put("mobile_no", mob);
// url = url+"?first_name="+fname+"&last_name="+lname+"&email="+email+"&mob_no="+mob;
Log.d("URL -- ", "" + url);
GsonRequest<AddtoWishlistResponse> myReq = new GsonRequest<AddtoWishlistResponse>(
Request.Method.PATCH,
url,
AddtoWishlistResponse.class,
headers,
params,
createMyReqSuccessListener(),
createMyReqErrorListener());
AppController.getInstance().addToRequestQueue(myReq, tag_json_obj);
}
【问题讨论】:
-
尝试使用
getBody()而不是getParams() -
@BNK 你应该定义一个方法 getBody 还是如何调用它?
-
我已经添加了我的答案,但是,为什么你说
make PUT request但在代码中,你使用Request.Method.PATCH?
标签: android gson android-volley