3

Volley JsonObjectRequest 中报告了崩溃,并且恰好在其onErrorResponse块中。我无法将 web 服务输出更改为测试onErrorResponse块,调试此代码块很重要。

谁能告诉我如何强制代码进入这部分?

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

4

1 回答 1

3

正如@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

    }
});
于 2018-12-08T12:21:33.173 回答