我是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