我是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日志? – tebitoq
你有没有找到onErrorResponse()? –
我解决了它,但它有点奇怪。内容长度是api期待的问题96.但是我发送了124.但是,124这是邮递员的工作。有人可以解释原因吗? – Stackover67