【发布时间】:2017-01-07 21:49:46
【问题描述】:
在这里我想了解为什么getHeaders() 和getParams() 函数没有被使用和工作。?
private String tag_json_obj = "jobj_req";
ProgressDialog pDialog = new ProgressDialog(this);
String tag_json_obj = "json_obj_req";
String url = "https://api.abc.xyz.com:3021/login";
pDialog.setMessage("Loading...");
pDialog.show();
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
url, obj,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(AbcLogin.class.getSimpleName(), response.toString());
pDialog.hide();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(AbcLogin.class.getSimpleName(), "Error: " + error.getMessage());
// hide the progress dialog
pDialog.hide();
}
});
/**
* Passing some request headers
* */
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");
return headers;
}
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("user", "abc12");
params.put("password", "xyzan");
return params;
}
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
我必须在代码中同时传递标头和参数。
【问题讨论】:
-
使用 StringRequest 代替 JsonObjectRequest
-
我尝试使用 StringRequest 但它说无法解析构造函数。
标签: java android api httprequest android-volley