凌空不解析404响应 [英] Volley Not Parsing 404 response

查看:141
本文介绍了凌空不解析404响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当服务器返回404响应时,Volley将返回错误,即使该404响应包含基于json的错误代码也是如此.它不会解析包含jason的404响应{code:resourceNotFound,msg:message_index}

Volley returns an error when a 404 response is returned from the server even if that 404 response contains json based error codes. It does not parse the 404 response which contains jason { code: resourceNotFound, msg:message_index }

是否有让Volley处理404消息中的JSon的方法?当找不到资源时,与我集成的服务会返回404.

Is there anyway to get Volley to process the JSon in a 404 message? The service I am integrating with returns a 404 when a resource cannot be found.

推荐答案

如果您收到404响应,它应该进入您设置的任何错误侦听器.您在错误侦听器中得到一个VolleyError对象.您可以从此对象获取网络响应,然后从响应主体获取数据.它以char数组形式给出,因此您需要自己将其转换为其他形式.

If you get a 404 response it should get into whatever error listener you set. You get a VolleyError object in the error listener. You can get the network response from this object and then the data from the response body. It's given as a char array so you need to convert it to something else yourself.

下面的代码段是一个简单的请求,您需要输入自己的URL.

The snippet below is a simple request that does that, you'll need to put in your own URL though.

    StringRequest request = new StringRequest( Request.Method.GET, "yourURL", new Response.Listener<String>() {
        @Override
        public void onResponse( String s ) {
            //Do whatever
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse( VolleyError volleyError ) {
            try {
                String responseBody = new String( volleyError.networkResponse.data, "utf-8" );
                JSONObject jsonObject = new JSONObject( responseBody );
            } catch ( JSONException e ) {
                //Handle a malformed json response
            } catch (UnsupportedEncodingException error){

            }
        }
    }
    );

这篇关于凌空不解析404响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆