2017-08-25 58 views
-1

GET請求解析響應我有麻煩解析中的Android以下響應時:麻煩與來自Android中

{"success":true,"msg":"Okay","result":{"content_type_id":"2","Content_Type_Name":"Media"}} 

我需要有content_type_id檢索,所以可在一組方法中使用它。

StringRequest stringRequest = new StringRequest(Request.Method.GET, url, 
     new Response.Listener<String>() { 
      @Override 
      public void onResponse(String response) { 
       // This is where I'm stuck. 
      } 

一些我are not able to convert to JSONArrayno value for錯誤的。

我該如何解決這個問題?

回答

1

使用下面的代碼, 寫在代碼裏onResponse方法。

try { 
     JSONObject jsonObject=new JSONObject(response); 

     JSONObject resultObject = jsonObject.has("result") ? jsonObject.optJSONObject("result") : new JSONObject(); 
     String contentId = resultObject.has("content_type_id") ? resultObject.optString("content_type_id") : ""; 

     Log.d("content_type_id", contentId); 

    } 
    catch (Exception ex) 
    { 
     ex.printStackTrace(); 
    } 

希望它能幫助你。

+0

謝謝@Vishal Chhodwani,這對我有用。 –

+0

快樂都是我的:) –

0
JsonObject jsonObject=new JsonObject(response); 
+0

我確實有這個,但是如何從這裏獲取content_type_id? –

+0

jsonObject.getJSONObject(「result」)。getString(「content_type_id」); –

0

響應中沒有JSONArray。轉換成JSONObject。

JsonObject jsonObject=new JsonObject(response); 
0

使用JsonObjectRequest更換到StringRequest;

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET, url, jsonObj, 
      new Response.Listener<JSONObject>() { 

       @Override 
       public void onResponse(JSONObject response) { 

        printlog("response", response + ""); 
       } 
      }, new Response.ErrorListener() { 

     @Override 
     public void onErrorResponse(VolleyError volleyError) { 
      callback.serviceCallback(callbackId, null); 

      String message = null; 
      if (volleyError instanceof NetworkError) { 
       message = "Cannot connect to Internet...Please check your connection!"; 
       Toast.makeText(getInstance(),message,Toast.LENGTH_SHORT).show(); 
      } else if (volleyError instanceof ServerError) { 
       message = "The server could not be found. Please try again after some time!!"; 
       Toast.makeText(getInstance(),message,Toast.LENGTH_SHORT).show(); 
      } else if (volleyError instanceof AuthFailureError) { 
       message = "Cannot connect to Internet...Please check your connection!"; 
       Toast.makeText(getInstance(),message,Toast.LENGTH_SHORT).show(); 
      } 
      else if (volleyError instanceof NoConnectionError) { 
       message = "Cannot connect to Internet...Please check your connection!"; 
       Toast.makeText(getInstance(),message,Toast.LENGTH_SHORT).show(); 
      } else if (volleyError instanceof TimeoutError) { 
       message = "Connection TimeOut! Please check your internet connection."; 
       Toast.makeText(getInstance(),message,Toast.LENGTH_SHORT).show(); 
      } 
     } 
    }); 


    jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(5000 * 60, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, 
      DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); 
    // Adding request to request queue 
    try { 
     getInstance().addToRequestQueue(jsonObjReq, url); 

    } catch (NullPointerException e) { 
     e.printStackTrace(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
0

其他人正在跟蹤,但你需要進一步。 Vishal Chhodwani也有很好的答案,它包含了一些驗證。

首先,按照他們的建議將響應轉換爲JSONObject。然後從響應中檢索結果作爲JSONObject。最後,從結果中獲取content_type_id。

JSONObject jsonResponse = new JSONObject(response); 
JSONObject jsonResult = jsonResponse.getJSONObject("result"); 
String contentTypeID = jsonResult.getString("content_type_id"); 

此外,如果你期待content_type_id成爲你的一套方法的int,可以改爲嘗試這個辦法:

int contentTypeID = jsonResult.getInt("content_type_id"); 

更詳細的信息,這裏是Android的一個鏈接JSONObject的文檔:https://developer.android.com/reference/org/json/JSONObject.html