凌空永远不会返回VolleyError或解释了为什么有一个例外 [英] Volley never returns VolleyError or explains why there is an exception

查看:173
本文介绍了凌空永远不会返回VolleyError或解释了为什么有一个例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过凌空库,我延长了Request对象来实现系列化GSON。然后,我延长了我要怎么做一些我的的PUT 请求新对象。这是GSON序列的第一个对象

  @覆盖
受保护的响应< T> parseNetworkResponse(NetworkResponse响应){
    尝试{
        字符串jsonString =新的String(response.data,HttpHeaderParser.parseCharset(response.headers)); //响应可能是字符串是否过大?
        ŧparsedGSON = mGson.fromJson(jsonString,CLS);
        响应< T> returnMessage = Response.success(parsedGSON,
                HttpHeaderParser.parseIgnoreCacheHeaders(响应));
        返回returnMessage;    }赶上(UnsupportedEncodingException五){
        e.printStackTrace();
        返回Response.error(新ParseError(e)条);
    }赶上(JsonSyntaxException JE){
        je.printStackTrace();
        Log.e(GsonRequest,je.getMessage()= NULL je.getMessage():!?JsonSyntaxError);
        返回Response.error(新ParseError(JE));
    }
}

在我的网络响应到达响应< T> returnMessage = Response.success(parsedGSON,HttpHeaderParser.parseIgnoreCacheHeaders(响应)); 我已填充< T> 用正确的类的对象我通过在完全系列化与所有变量和没有错误。然而,出于某种原因,凌空跳跃到}赶上(JsonSyntaxException JE){,我不能透露的内容 JE 调试断点或打印日志。此外,在我的扩展类:

 新ErrorListener(){
        @燮pressWarnings(未使用)
        @覆盖
        公共无效onErrorResponse(VolleyError错误){
            error.printStackTrace();

onErrorResponse 不会被调用(既不是我的 onResponse 部分其一)

所以,现在我不知道为什么乱射被捉住JSONException,系列化的时候是成功的,我不知道为什么凌空没有返回的错误对象

洞察AP preciated


解决方案

答案是任何凌空功能,您扩展或覆盖必须有

  @覆盖
保护无效deliverResponse(T响应){
    // TODO自动生成方法存根
    mListener.onResponse(响应);
}

函数实现的。监听器必须在构造函数初始化,并实施了onResponse方法。

否则你的网络电话将永远不会在 onResponse 部分返回。

编辑:
和你的扩展请求类有,也能实现deliverError,与deliverResponse沿

 私人最终听者LT; T> mListener;
私人ErrorListener mErrorListener;@覆盖
 公共无效deliverError(VolleyError错误){
    mErrorListener.onErrorResponse(错误);
}

ErrorListener 在构造函数初始化

With the Volley libraries, I extended the Request object to implement GSON serialization. I then extended that new object for how I want to do some of my PUT requests. This is the first object for the GSON serialization:

@Override  
protected Response<t> parseNetworkResponse(NetworkResponse response) {  
    try {  
        String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); //response may be too large for string?
        t parsedGSON = mGson.fromJson(jsonString, cls);
        Response <t> returnMessage = Response.success(parsedGSON,  
                HttpHeaderParser.parseIgnoreCacheHeaders(response));
        return returnMessage;

    } catch (UnsupportedEncodingException e) { 
        e.printStackTrace();
        return Response.error(new ParseError(e));  
    } catch (JsonSyntaxException je) {
        je.printStackTrace();
        Log.e("GsonRequest", je.getMessage()!=null?je.getMessage():"JsonSyntaxError");
        return Response.error(new ParseError(je));  
    }  
}  

When my network response gets to Response <t> returnMessage = Response.success(parsedGSON, HttpHeaderParser.parseIgnoreCacheHeaders(response)); I have populated <t> objects with the correct classes I passed in completely serialized with all variables and no errors. Yet for some reason Volley jumps to } catch (JsonSyntaxException je) { and I can't reveal the contents of je with debugging breakpoints or printing logs. Also in my extended class:

 new ErrorListener() {
        @SuppressWarnings("unused")
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();

The onErrorResponse is never called (neither is my onResponse section either)

So now I have no idea why Volley is catching a JSONException, when serialization was successful, and I have no idea why Volley isn't returning the Error objects

Insight appreciated

解决方案

The answer is that any Volley function you extend or override must have

    @Override
protected void deliverResponse(T response) {
    // TODO Auto-generated method stub
    mListener.onResponse(response);
}

function implemented. The Listener must be initialized in the constructor and have the onResponse method implemented.

Otherwise your network call will never return in the onResponse section.

EDIT: and your extended Request class has to also implement deliverError, along with deliverResponse

private final Listener<T> mListener;
private ErrorListener mErrorListener;

@Override
 public void deliverError(VolleyError error) {
    mErrorListener.onErrorResponse(error);
}

with ErrorListener initialized in your constructor

这篇关于凌空永远不会返回VolleyError或解释了为什么有一个例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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