【发布时间】:2021-09-23 23:49:39
【问题描述】:
我正在尝试使用 volley 将数据传递给 api,如下所示
RequestQueue requestQueue = Volley.newRequestQueue(context);
Map<String, String> params = new HashMap();
params.put("fullName","some name");
params.put("phone","some phone number");
JSONObject parameters = new JSONObject(params);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url,parameters, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("jdjd","response = "+response);
}
}
, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("jdjd","response_error = "+error);
}
})
{
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("api-key", "my_api_key");
return params;
}
};
requestQueue.add(jsonObjectRequest);
我也试过
params.put("Content-Type", "application/json; charset=utf-8");
然后添加
@Override
public String getBodyContentType() {
return "application/json";
}
任何帮助将不胜感激
【问题讨论】: