【问题标题】:Trouble with parsing response from GET request in Android在 Android 中解析来自 GET 请求的响应时出现问题
【发布时间】:2017-08-25 12:58:08
【问题描述】:

在 Android 中解析以下响应时遇到问题:

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

我需要检索 content_type_id,所以我可以在 set 方法中使用它。

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

我该如何解决这个问题?

【问题讨论】:

    标签: android json parsing android-volley


    【解决方案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();
        }
    

    希望对你有所帮助。

    【讨论】:

    • 快乐是我的 :)
    【解决方案2】:

    您的回复中没有 JSONArray。转换成 JSONObject。

    JsonObject jsonObject=new JsonObject(response);
    

    【讨论】:

      【解决方案3】:

      使用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();
          }
      

      【讨论】:

        【解决方案4】:

        其他人都走上了正轨,但你需要走得更远。 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 成为您的 set 方法的 int,您可以试试这个:

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

        更多详细信息,这里是 JSONObject 的 Android 文档的链接:https://developer.android.com/reference/org/json/JSONObject.html

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-04-14
          • 2019-07-01
          • 2013-02-12
          • 1970-01-01
          • 2015-02-03
          • 1970-01-01
          相关资源
          最近更新 更多