Volley -JsonArrayRequest显示错误

时间:2018-11-07 06:34:29

标签: android android-volley

这是我的代码,每当我尝试使Response转换为toString()时,我都会得到错误

private void fetchStoreItems() {
        String url = "https://newsapi.org/v1/articles?source=techcrunch&apiKey=47bdfe44632849b4bdf0c2a9035a68e7";
        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET,
                url, new com.android.volley.Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                VolleyLog.d("ecardCalled: ", response.toString());
            }
        },new com.android.volley.Response.ErrorListener(){

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

        MyApplication.getInstance().addToRequestQueue(jsonArrayRequest);
    }

enter image description here

我实际上在做什么不对?

2 个答案:

答案 0 :(得分:1)

如果您将其设置为params,则必须提供json null

 JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET,
                url,null, new com.android.volley.Response.Listener<JSONArray>() {
            @Override
            ....
            ....
            ...
            //other code

答案 1 :(得分:1)

使用此代码代替您的代码:

 private void fetchStoreItems() {
    String url = "https://newsapi.org/v1/articles?source=techcrunch&apiKey=47bdfe44632849b4bdf0c2a9035a68e7";
   JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
       @Override
       public void onResponse(JSONArray response) {
           VolleyLog.d("ecardCalled: ", response.toString());
       }
   }, new Response.ErrorListener() {
       @Override
       public void onErrorResponse(VolleyError error) {
           VolleyLog.d("ecardCalled: ", error.toString());
       }
   });

    MyApplication.getInstance().addToRequestQueue(jsonArrayRequest);
}