现在的位置: 首页 > 综合 > 正文

赠送 HttpClient 和HttpURLConnection 的轻型网络框架 —》常见异常分析

2018年02月17日 ⁄ 综合 ⁄ 共 4972字 ⁄ 字号 评论关闭

转载注明出处
http://blog.csdn.net/codingandroid/article/details/41749581

之前一篇中有使用到AsyncBaseRequest 类,其中抛出了很多异常,当然已经有了部分注释,那下面我们来分析一下这些异常,以及异常出现的情况

ConnectTimeoutException 这个是连接异常(例如根本没有打开网络等情况下),数据还没有到达服务器,所以就算是订单类的也没有关系,可以再次提交

这个情况比较好处理了,就是请求压根没到服务器,如果要模拟这个错误最好的办法就是,你局域网打开服务,把手机改为3g模式下,或者填写一个压根不存在的 服务地址。当然现实情况下也可能出现,信号极差的情况下等,这种情况出现,提示超时,让用户再来一次就OK.

SocketTimeoutException 这个超时异常是说明请求已经到达服务器,返回数据过程中超时了,如果是订单类的请求要注意了,需要防止重复提交

这个异常出现场景就是 例如你的超时时间是1s,当数据到达服务器,服务器1s还没有能够顺利的返回数据,这时候已经到达了客户端设置的超时时间,那么,这个请求就超时了,但是注意,你的请求这时候已经提到了服务器,虽然你本地异常已经抛出了,服务器这时候依然在处理,如果你是去修改的某个字段,服务器可能已经修改成功了,而你本地看到的是异常,这时候可以把超时时间放大一点,或者服务器处理数据的速度需要去优化

TimeoutException 这个是前两个的父类 也就是说你如果要求不是很严格的情况下,可以忽略上面两种,直接捕获这个,当然作为框架,我希望能详细一点,要不直接捕获一个Exception就完事儿了,当然捕获了前面两个,我们还担心会有其他的TimeOut情况呢,所以还是再捕获一下,以防万一

HttpHostConnectException 这个异常就不说了,通常是你没有加权限

NoHttpResponseException 这个是DefaultHttpClient 会出现的,属于android下Httpclient的一个缺陷 可以参考  http://stackoverflow.com/questions/2052299/httpclient-on-android-nohttpresponseexception-through-umts-3g 
不过

HttpProtocolParams.setUseExpectContinue(httpClient.getParams(), false);

这个我加了,貌似不管用 ,然后上面这种重试的方法貌似也没有起作用,感兴趣的朋友可以研究一下哦,我遇到就直接 抓住再抛出一个 TimeOut异常出去了

EOFException 这个异常就头疼了,是HttpURLConnection 的缺陷 http://bbs.csdn.net/topics/390143086这个是csdn的帖子
还有 http://stackoverflow.com/questions/15411213/android-httpsurlconnection-eofexception  几乎所有的处理方法我都加上了,还是概率性的会出现这个异常,那不管了,直接抛出TimeOut
糊弄糊弄用户吧,不过我们开发者自己要了解哦

IOException  流读取的时候错误了,那时候我们认为是超时  然后其他异常就不去管了

package com.clxu.netframe.exception;

import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.LinkedList;
import java.util.concurrent.TimeoutException;

import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.HttpHostConnectException;

import android.util.Log;

import com.clxu.netframe.R;
import com.clxu.netframe.constant.Constant;

/** @类名: MyException
 * @功能描述: TODO(自定义异常基类)
 * @创建人: Clxu
 * @创建时间: 2014-5-28 上午8:24:56
 */
public class MyException extends Exception {
    
    private static final long serialVersionUID = 7803623314130181679L;

    /**
     * 错误异常代码
     */
    protected int errorCode = -1;
    /**
     * 界面异常提示信息
     */
    private int msg;

    /**
     * 开发人员查看的简单异常信息
     */
    protected int errorMsg;
    /**
     * 开发人员查看的简单异常信息
     */
    protected String errorMsgInfo;
    
    /**
     * 原始异常信息
     */
    protected Exception oldException;
    /**
     * 异常链(捕获一个地方抛出的多个异常)
     */
    protected LinkedList<Exception> list = new LinkedList<Exception>();
    
    public MyException () {};
    
    public MyException (String message) {
        super(message);
        errorMsgInfo=message;
    }
    
    public MyException (int message) {
        this.setMsg(message);
        this.errorMsg = message;
    };

    public MyException (int message, int errorCode) {
        this.setMsg(message);
        this.errorMsg = message;
        this.errorCode = errorCode;
    };
    
    public MyException (int message, int errorCode, int errorMsg) {
        this.setMsg(message);
        this.errorMsg = errorMsg;
        this.errorCode = errorCode;
    };
    /**
     * 应该new子类,谁使用谁定义
     * @param e
     */
    public MyException (Exception e) {
        if (e instanceof IOException) {
            this.errorCode = Constant.NETWORK_REQUEST_IOEXCEPTION_CODE;
            this.setMsg(R.string.network_request_ioexception);
            this.errorMsg = R.string.network_request_ioexception;
        }  else if (e instanceof SocketTimeoutException) {//超时
            this.errorCode = Constant.NETWORK_REQUEST_SOCKET_EXCEPTION;
            this.setMsg(R.string.network_request_ioexception);
            this.errorMsg = R.string.network_request_ioexception;
        } else if (e instanceof ConnectTimeoutException) {//超时
            this.errorCode = Constant.NETWORK_REQUEST_CONNECT_EXCEPTION;
            this.setMsg(R.string.network_request_ioexception);
            this.errorMsg = R.string.network_request_ioexception;
        } else if (e instanceof TimeoutException) {//超时
            this.errorCode = Constant.NETWORK_REQUEST_TIMEOUT_EXCEPTION;
            this.setMsg(R.string.network_request_ioexception);
            this.errorMsg = R.string.network_request_ioexception;
        }else if (e instanceof HttpHostConnectException) {
            this.errorCode = Constant.NETWORK_REQUEST_PERRMIT_EXCEPTION;
            this.setMsg(R.string.network_request_ioexception);
            this.errorMsg = R.string.network_request_ioexception;
        }else if (e instanceof Exception) {
            this.errorCode = Constant.NETWORK_REQUEST_UNKNOWN_EXCEPTION;
            this.setMsg(R.string.system_exception);
            this.errorMsg = R.string.unknown_exception;
        }
        this.oldException = e;
        printMessage(e);
    }
    
    /**
     * 将异常添加到异常链
     * @param e
     */
    public void addException(Exception e) {
        list.add(e);
    }
    /**
     * 获得最底层的异常
     * @return
     */
    public Exception getFirstException() {
        if(list.size() > 0)
            return list.get(0);
        else
            return null;
    }
    /**
     * 打印异常信息
     */
    public void printMessage(Exception e) {
        e.printStackTrace();
    }
    /**
     * 记录异常信息
     */
    public void logMessage() {
        Log.e("Exception",getDetailMessage());
    }
    /**
     * 获得异常代码(界面层直接调用getMessage或showMessage即可)
     * @return
     */
    public int getErrorCode() {
        return errorCode;
    }
    /**
     * 获得开发人员查看的简单异常信息
     * @return
     */
    public int getErrorMsg() {
        return errorMsg;
    }
    /**
     * 获得原始异常信息
     * @return
     */
    public String getDetailMessage() {
        return oldException.getMessage();
    }
    /**
     * 获得异常链
     * @return
     */
    public LinkedList<Exception> getList() {
        return list;
    }

    public int getMsg() {
        return msg;
    }
 
    public void setMsg(int msg) {
        this.msg = msg;
    }
    public String  getErrorMsgInfo(){
    	if(msg==0){
    		return errorMsgInfo;
    	}else{   		
    		return "resource not found";
    	}
    }
}

详细看文章的会说为什么这里面没有NoHttpResponseException  EOFException
这两个异常 ,这两个异常我是在请求网络的时候直接去处理的,抓到之后直接抛出 Timeout

try
{
response = client.execute(post);
} catch (NoHttpResponseException e) {
throw new TimeoutException();
}

这样去处理的哦,好了,异常就说到这里了

框架源码下载地址:http://download.csdn.net/detail/brightshadow11111/8228287

抱歉!评论已关闭.