【问题标题】:Volley JSON Response排球 JSON 响应
【发布时间】:2017-12-27 12:23:44
【问题描述】:

我想在我的代码中使用 Volley,我不明白我应该在下面的响应中使用哪种请求类型。 网址:::http://localhost/api/product/read.php

    {
      "data":[
        {
         "category_id":"1",
         "category_name":"Today's Recipe",
         "recipes":[
          {
           "id":"1",
           "recipe_name":"Peanut, and Chilli Noodles",
           "ingredients":"Serves: 4 \r\n\r\n250g (9 oz) fresh Chinese ,
           "prepration":"Prep:15min Cook:10min Ready in:25min \r\n\r\nCook 
           noodles in a large pot of boiling water until done.  have 
           chilli paste, use minced red chilli to taste."
     }
    ]
  },

【问题讨论】:

  • 这似乎是一个 JSON 对象,因此您应该使用 JsonObjectRequest developer.android.com/training/volley/request.html
  • 你应该使用 JsonRequest。请参阅我在此博客上的回答:stackoverflow.com/questions/47936955/…
  • 当我使用 jsonobject 请求时出现以下错误显示::: com.android.volley.ParseError: org.json.JSONException: java.lang.String 类型的值连接无法转换为 JSONObject
  • 我看到最后一个逗号是这个错字还是真的是一个不完整的数组?
  • 我怀疑json数据不正确。 data 数组未关闭,我猜 "prepration"ingredients 属性的一部分。仅供参考,您的 localhost 网址仅适用于您而不是我们,因为服务器已部署在您的系统中。

标签: android android-volley


【解决方案1】:

试试这个:

RequestQueue requestQueue = Volley.newRequestQueue(context);
    final String url = "url here";

    JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONObject>()
            {
                @Override
                public void onResponse(JSONObject response)
                {
                Log.e(" result",(String.valueOf(response)));
                }
            },
            new Response.ErrorListener()
            {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }
    );
   requestQueue.add(getRequest);

希望这会有所帮助。

【讨论】:

【解决方案2】:
StringRequest jsonObjectRequest = new StringRequest(Request.Method.POST, url,  new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                if (response != null) {
                    try {
JSONObject jsonObject = new JSONObject(response);
                        if (jsonObject.getString("status").equalsIgnoreCase("success")) {
                    JSONArray jsonArray = jsonObject.getJSONArray("data"); 
if(jsonArray!=null && jsonArray.length()>0){
    for(int i=0;i<jsonArray.length();i++){
JSONObject data_object=jsonArray.getJSONObject(i);
JSONArray recepie = data_object.getJSONArray("recipes"); 

}
//                           
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                }

        });
        MySingleton.getInstance(context).addToRequestQueue(jsonObjectRequest);
        jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(
                APIConstants.API_TIMEOUT, //for 30 Seconds
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    }

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2020-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    • 1970-01-01
    • 1970-01-01
    • 2017-08-10
    • 2020-12-17
    相关资源
    最近更新 更多