簡體   English   中英

如何讀取POST凌空的響應?

[英]How to read response from a POST volley?

我在Android上真的很新,而且我正在使用登錄系統,我正在使用凌空來發布數據......我遇到的問題是當我嘗試閱讀響應時....響應看起來像這樣:

{ “ST”: “無”, “消息”: “錯誤”}

我試圖只訪問st或消息有沒有辦法做到這一點? 我試過做:

response [i] ----找到的數組類型'org.json.JSONObject'

 JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, url, params, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) { 
                    Log.e(TAG, "Response: " + response.length());
                    for (int i = 0; i < response.length(); i++) {
                        Log.e(TAG, "Values: " + response);
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    error.printStackTrace(); 
                }
            });

            Volley.newRequestQueue(this).add(jsonRequest);

你可以用

response.getString("Message")

從給定的JSON獲取消息

 @Override
 public void onResponse(String response) {
    try {
           JSONObject api_response = new JSONObject(response);
           String message = api_response.getString("Message")
       } catch (JSONException e) {
            e.printStackTrace();           
       }

捕獲json異常非常重要,以防響應字符串無法轉換為json對象。 然后使用getString()從創建的json對象中獲取消息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM