Android Volley 的 onResponse 方法未执行

onResponse method of Android Volley is not executed

我正在使用 Volley 进行联网并连接到我的 REST API。以下代码显示了我的 MainActivity 的 onCreate 方法。在这里,我想向我的 url 发出 GET 请求并接收 JSON 信息。但它不起作用。我在整个代码中设置了不同的调试点。最后执行的是 arrReq 的初始化。 onResponse和onErrorResponse没有执行,不知道怎么回事

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    setupToolBar();
    setupPagerAdapter();
    setupFloatingButton();
    getDeviceId();

    JsonArrayRequest arrReq = new JsonArrayRequest(com.android.volley.Request.Method.GET, "http://localhost:8888/api/entities", null,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    // Check the length of our response (to see if there are any entities)
                    if (response.length() > 0) {
                        // There are entities so let's loop through them all.
                        for (int i = 0; i < response.length(); i++) {
                            try {
                                // Get each entity
                                JSONObject jsonObj = response.getJSONObject(i);
                                String locData = jsonObj.get("entity").toString();
                            } catch (JSONException e) {
                                // If there is an error then output this to the logs.
                                Log.e("Volley", "Invalid JSON Object.");
                            }

                        }
                    } else {
                        // There aren't any entities. So do nothing!
                    }

                }
            },

            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // If there a HTTP error then add a note to our list.
                    Log.e("Volley", error.toString());
                }
            }
    );
}

当我在设备上的浏览器中执行 url 时,收到 JSON 代码:

{
   "status":"success",
   "data":[
      {
         "entity":"(17,)"
      },
      {
         "entity":"(21,)"
      },
      {
         "entity":"(201,)"
      }
   ],
   "message":"Retrieved entities"
}

日志没有显示任何错误。

JsonArrayRequest request = new JsonArrayRequest (Request.Method.GET, URL, new Response.Listener<String>() {
        @Override
        public void onResponse(JSONArray response) {
            if (!response.equals(null)) {

                Log.e("Your Array Response", response);
            } else {
                Log.e("Your Array Response", "Data Null");
            }
        }

    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("error is ", "" + error);
        }
    }) {

        //This is for Headers If You Needed
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            //Map<String, String> params = new HashMap<String, String>();
           // params.put("Content-Type", "application/json; charset=UTF-8");
           // params.put("Authorization", "bearer  " + token);
            return params;
        }

        //Pass Your Parameters here
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            /*params.put("User", UserName);
            params.put("Pass", PassWord);*/
            return params;
        }
    };
    RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
    queue.add(request);

试试这个

可能是权限问题, 您是否在 manifest.xml 文件中添加了以下代码:

<uses-permission android:name="android.permission.INTERNET" /> 

终于明白了。问题是我得到了 JSONObject 作为响应,但实例化了 JSONArrayRequest。当我将其更改为 JSONObjectRequest 时,它工作得很好。