Volley JsonObjectRequest错误响应

时间:2018-03-07 10:31:04

标签: android android-volley jsonresponse

我有一个简单的Json对象请求类,这是调用整个请求的方法,而在LogCat中我只得到:

Volley: [2] 2.onErrorResponse: Error:

所以我不知道在哪里寻找修复

private void getBeerDetails() {
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
            (Request.Method.GET,
                    "https://api.punkapi.com/v2/beers/13",
                    null, new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    try {
                        beerName.setText(response.getString("name"));
                        alc.setText(response.getString("abv"));
                        ibu.setText(response.getString("ibu"));
                        firstBrewed.setText(response.getString("first_brewed"));
                        yeast.setText(response.getString("yeast"));
                        description.setText(response.getString("description"));
                        foodPairing.setText(response.getString("food_pairing"));

                        Picasso.with(getApplicationContext())
                                .load(response.getString("image_url"))
                                .into(beerImageView);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d("Error:", error.getMessage());
                }
            });

    requestQueue.add(jsonObjectRequest);
}

2 个答案:

答案 0 :(得分:2)

正如我在此处看到的,您的回复是JsonArray格式。 请尝试使用以下

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li>
    <div class="test-cards-list-item-title-actions">
      <div class="test-toggleswitch">
        <input type="checkbox" name="toggleswitch" class="test-toggleswitch-checkbox" id="mytoggleswitch1">
        <label class="test-toggleswitch-label" for="mytoggleswitch1"><span class="test-toggleswitch-inner test-unchecked">SHORTLIST</span><span class="test-toggleswitch-inner test-checked test-hidden">SHORTLISTED</span></label>
      </div>
    </div>
  </li>
  <li>
    <div class="test-cards-list-item-title-actions">
      <div class="test-toggleswitch">
        <input type="checkbox" name="toggleswitch" class="test-toggleswitch-checkbox" id="mytoggleswitch2">
        <label class="test-toggleswitch-label" for="mytoggleswitch2"><span class="test-toggleswitch-inner test-unchecked">SHORTLIST</span><span class="test-toggleswitch-inner test-checked test-hidden">SHORTLISTED</span></label>
      </div>
    </div>
  </li>
  <li>
    <div class="test-cards-list-item-title-actions">
      <div class="test-toggleswitch">
        <input type="checkbox" name="toggleswitch" class="test-toggleswitch-checkbox" id="mytoggleswitch3">
        <label class="test-toggleswitch-label" for="mytoggleswitch3"><span class="test-toggleswitch-inner test-unchecked">SHORTLIST</span><span class="test-toggleswitch-inner test-checked test-hidden">SHORTLISTED</span></label>
      </div>
    </div>
  </li>
  <li>
    <div class="test-cards-list-item-title-actions">
      <div class="test-toggleswitch">
        <input type="checkbox" name="toggleswitch" class="test-toggleswitch-checkbox" id="mytoggleswitch4">
        <label class="test-toggleswitch-label" for="mytoggleswitch4"><span class="test-toggleswitch-inner test-unchecked">SHORTLIST</span><span class="test-toggleswitch-inner test-checked test-hidden">SHORTLISTED</span></label>
      </div>
    </div>
  </li>
</ul>

答案 1 :(得分:1)

您的错误消息不是很具描述性。但是,我认为您需要在GET请求的标头中设置content-typeapplication/json。创建请求时覆盖getHeaders函数。这是一个java示例。

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, jsonBody,
    new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            Log.d("TAG", response.toString());
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("TAG", error.getMessage(), error);
        }
    }){

    @Override 
    public Map<String, String> getHeaders() throws AuthFailureError { 
        Map<String, String> params = new HashMap<String, String>();                
        params.put("Content-Type", "application/json");
        return params; 
    } 
};

<强>更新

根据您在答案中的评论,您无法解析从GET请求返回的JSON。正如我从您的响应JSON中看到的那样,您正在接收一组对象。我想建议使用Gson进行JSON解析。它简单易行。您需要定义一个类,其中包含数组中对象的字段,该字段在响应中返回。然后只需使用Gson将JSON数组中的值转换为您创建的特定对象的数组。这是一个样本。

Gson gson = new Gson();
Data[] dataArray = gson.fromJson(jsonLine, Data[].class);