在volley jsonobjectrequest及其onErrorResponse块中报告崩溃。我无法将webservice输出更改为testonErrorResponse块,调试此代码块很重要。
有人能告诉我如何强制代码进入这个部分吗?

JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, "MY_URL", null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {

            // Which code should I add here to force the code to go to onErrorResponse

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

            ...
            // I want to debug this code block

        }
    });

我在response = null;中尝试了onResponse,但它导致了崩溃,而且没有转到onErrorResponse

最佳答案

正如@Vivek Mishra在评论中所说,我试图设置一个无效的url,比如"https://stackoverflow.com/",而不是"MY_URL"(这是一个有效的api url),它转到了onErrorResponse

JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, "https://google.com/", null, new Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject response) {

        // OnResponse Codes

    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {

        // OnError Codes

    }
});

08-26 07:37