我的服務回應是一個 JsonArray,但我發送的正文應該是一個 jsonObject。但是當我嘗試時,它不接受 postData(它在請求行中將下劃線標記為紅色)那么我該如何解決呢?
我的服務資料來了,但是當我使用 JsonobjectRequest 并給出錯誤時,它會轉到 one rrorResponse 函式:com.android.volley.ParseError: org.json.JSONException: Value[{"total":120, ....}]
并且在控制臺詳細訊息中是: org.json.JSONArray 型別的值 [{“total”:120, ....}] 無法轉換為 JSONObject
代碼是這樣的:
GetDataFromService()
{
JSONArray xClasses = new JSONArray();
JSONArray xTypes = new JSONArray();
JSONArray xGroups = new JSONArray();
JSONArray yTypes = new JSONArray();
JSONObject filteredItems = new JSONObject();
try {
filteredItems.put("level", 0);
filteredItems.put("subLevel", 0);
filteredItems.put("id", 0);
} catch (JSONException e) {
e.printStackTrace();
}
JSONObject postData = new JSONObject();
try
{
postData.put("FilteredItems", filteredItems);
postData.put("XClasses", xClasses);
postData.put("XTypes", xTypes);
postData.put("XGroups", xGroups);
postData.put("YTypes", yTypes);
}
catch (JSONException e)
{
e.printStackTrace();
}
JsonArrayRequest postRequest = new JsonArrayRequest(Request.Method.POST, url, postData,
new Response.Listener<JSONArray>()
{
@Override
public void onResponse(JSONArray response)
{
try {
JSONArray json = new JSONArray(response);
System.out.println(json);
} catch (JSONException e) {
e.printStackTrace();
}
Gson gson = new Gson();
System.out.println(response); }
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
// TODO Auto-generated method stub
Log.d("ERROR","error => " error.toString());
}
}
)
{ // Bearer Token - for Authorization
@Override
public Map<String, String> getHeaders() throws AuthFailureError
{
Map<String, String> params = new HashMap<>();
params.put("Authorization","Bearer " token);
return params;
}
};
myQueue.add(postRequest);
}
代碼給出了 postData 型別的錯誤。因為我將請求作為 jsonarray 發送,但發布資料是 jsonbject。
uj5u.com熱心網友回復:
似乎它在最近的 volley 版本中已被洗掉,但您可以輕松修改此建構式并添加到 JsonArrayRequest。
public JsonArrayRequest(int method, String url, JSONObject jsonRequest, Listener listener, ErrorListener errorListener) { super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener, errorListener); }
uj5u.com熱心網友回復:
JsonArrayRequest構造函式接受 JSONArray 作為引數。
嘗試將您的 JSONObject 轉換為 JSONArray
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/523059.html
標籤:爪哇安卓邮政json响应
