【发布时间】:2018-03-20 19:58:29
【问题描述】:
我是 Android 新手。我正在弄清楚如何进行网络调用。我正在尝试将标头与JSON 数据一起发送。我尝试调试代码,但它不会去onResponse()。有人可以告诉我我做错了什么。
下面是我的代码。
String url = "https://api.....";
RequestQueue requestQueue = Volley.newRequestQueue(this);
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("image", "https://i.imgur.com/5KdM.jpg");
jsonObject.put("subject_id", "Elixabeth");
jsonObject.put("gallery_name", "MyGallery");
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
if (response != null){
}
// do something...
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// do something...
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
final Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
headers.put("app_id", "");
headers.put("app_key", "");
headers.put("Content-Length", "124");
return headers;
}
};
requestQueue.add(jsonObjectRequest);
} catch (JSONException e) {
e.printStackTrace();
}
我用相同的数据在 Postman 中尝试了相同的操作,我得到了响应。
【问题讨论】:
-
您是否尝试记录 onErrorResponse 方法?你有一些 stackTrace 日志吗?
-
你到达 onErrorResponse() 了吗?
-
我解决了..但这有点奇怪。 Content-Length 是 api 期望 96 的问题。但我发送的是 124。但是 124 它在邮递员中工作。有人可以解释原因吗?
标签: java android android-volley