[英]invalid request error when trying to call a JSON api through android
我正在嘗試通過 android 調用 API,它給出了無效的請求錯誤。
而 API 在 POSTMAN 中給出了正確的結果。
我正在使用以下代碼。 以下代碼有問題。
String url ="http://apiaddress";
// POST parameters
Map<String, String> params = new HashMap<String, String>();
params.put("tag", "test");
JSONObject jsonObj = new JSONObject(params);
// Request a json response from the provided URL
JsonObjectRequest jsonObjRequest = new JsonObjectRequest
(Request.Method.POST, url, jsonObj, new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response)
{
System.out.print("JSOn Object:"+jsonObj.toString());
}
});
在 Volley 中, JsonObjectRequest
似乎不適用於 POST 請求。 請改用StringRequest
。
也檢查這個答案。
此外, override
StringRequest
的getParams
方法並將您的參數作為map
返回。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.