【ListViewJson】【com.demo.app】【AppException】源码分析及其在工程中作用

源码如下:

package com.demo.app;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.Thread.UncaughtExceptionHandler;
import java.net.ConnectException;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Date;

import org.apache.commons.httpclient.HttpException;

import com.demo.app.common.UIHelper;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.os.Environment;
import android.os.Looper;
import android.widget.Toast;

/**
 * 应用程序异常类:用于捕获异常和提示错误信息
 * @version 1.0
 * @created 2012-3-21
 */
public class AppException extends Exception implements UncaughtExceptionHandler{

    private final static boolean Debug = false;//是否保存错误日志

    /** 定义异常类型 */
    public final static byte TYPE_NETWORK     = 0x01;
    public final static byte TYPE_SOCKET    = 0x02;
    public final static byte TYPE_HTTP_CODE    = 0x03;
    public final static byte TYPE_HTTP_ERROR= 0x04;
    public final static byte TYPE_XML         = 0x05;
    public final static byte TYPE_IO         = 0x06;
    public final static byte TYPE_RUN         = 0x07;

    private byte type;
    private int code;

    /** 系统默认的UncaughtException处理类 */
    private Thread.UncaughtExceptionHandler mDefaultHandler;

    private AppException(){
        this.mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
    }

    private AppException(byte type, int code, Exception excp) {
        super(excp);
        this.type = type;
        this.code = code;
        if(Debug){
            this.saveErrorLog(excp);
        }
    }
    public int getCode() {
        return this.code;
    }
    public int getType() {
        return this.type;
    }

    /**
     * 保存异常日志
     * @param excp
     */
    public void saveErrorLog(Exception excp) {
        String errorlog = "errorlog.txt";
        String savePath = "";
        String logFilePath = "";
        FileWriter fw = null;
        PrintWriter pw = null;
        try {
            //判断是否挂载了SD卡
            String storageState = Environment.getExternalStorageState();
            if(storageState.equals(Environment.MEDIA_MOUNTED)){
                savePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/OSChina/Log/";
                File file = new File(savePath);
                if(!file.exists()){
                    file.mkdirs();
                }
                logFilePath = savePath + errorlog;
            }
            //没有挂载SD卡,无法写文件
            if(logFilePath == ""){
                return;
            }
            File logFile = new File(logFilePath);
            if (!logFile.exists()) {
                logFile.createNewFile();
            }
            fw = new FileWriter(logFile,true);
            pw = new PrintWriter(fw);
            pw.println("--------------------"+(new Date().toLocaleString())+"---------------------");
            excp.printStackTrace(pw);
            pw.close();
            fw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(pw != null){ pw.close(); }
            if(fw != null){ try { fw.close(); } catch (IOException e) { }}
        }

    }

    public static AppException http(int code) {
        return new AppException(TYPE_HTTP_CODE, code, null);
    }

    public static AppException http(Exception e) {
        return new AppException(TYPE_HTTP_ERROR, 0 ,e);
    }

    public static AppException socket(Exception e) {
        return new AppException(TYPE_SOCKET, 0 ,e);
    }

    public static AppException io(Exception e) {
        if(e instanceof UnknownHostException || e instanceof ConnectException){
            return new AppException(TYPE_NETWORK, 0, e);
        }
        else if(e instanceof IOException){
            return new AppException(TYPE_IO, 0 ,e);
        }
        return run(e);
    }

    public static AppException xml(Exception e) {
        return new AppException(TYPE_XML, 0, e);
    }

    public static AppException network(Exception e) {
        if(e instanceof UnknownHostException || e instanceof ConnectException){
            return new AppException(TYPE_NETWORK, 0, e);
        }
        else if(e instanceof HttpException){
            return http(e);
        }
        else if(e instanceof SocketException){
            return socket(e);
        }
        return http(e);
    }

    public static AppException run(Exception e) {
        return new AppException(TYPE_RUN, 0, e);
    }

    /**
     * 获取APP异常崩溃处理对象
     * @param context
     * @return
     */
    public static AppException getAppExceptionHandler(){
        return new AppException();
    }

    @Override
    public void uncaughtException(Thread thread, Throwable ex) {

        if(!handleException(ex) && mDefaultHandler != null) {
            mDefaultHandler.uncaughtException(thread, ex);
        }

    }
    /**
     * 自定义异常处理:收集错误信息&发送错误报告
     * @param ex
     * @return true:处理了该异常信息;否则返回false
     */
    private boolean handleException(Throwable ex) {
        if(ex == null) {
            return false;
        }

        final Context context = AppManager.getAppManager().currentActivity();

        if(context == null) {
            return false;
        }

        final String crashReport = getCrashReport(context, ex);
        //显示异常信息&发送报告
        new Thread() {
            public void run() {
                Looper.prepare();
                Looper.loop();
            }

        }.start();
        return true;
    }
    /**
     * 获取APP崩溃异常报告
     * @param ex
     * @return
     */
    private String getCrashReport(Context context, Throwable ex) {
        PackageInfo pinfo = ((AppContext)context.getApplicationContext()).getPackageInfo();
        StringBuffer exceptionStr = new StringBuffer();
        exceptionStr.append("Version: "+pinfo.versionName+"("+pinfo.versionCode+")\n");
        exceptionStr.append("Android: "+android.os.Build.VERSION.RELEASE+"("+android.os.Build.MODEL+")\n");
        exceptionStr.append("Exception: "+ex.getMessage()+"\n");
        StackTraceElement[] elements = ex.getStackTrace();
        for (int i = 0; i < elements.length; i++) {
            exceptionStr.append(elements[i].toString()+"\n");
        }
        return exceptionStr.toString();
    }
}
时间: 2024-11-08 17:29:54

【ListViewJson】【com.demo.app】【AppException】源码分析及其在工程中作用的相关文章

[ListViewJSON][com.demo.app][AppContext]源码分析及其在工程中的作用

首先插入AppContext源码 package com.demo.app; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InvalidClassException; import java.io.Object

【ListViewJSON】【com.demo.app.api】【ApiClient】源码分析及其在工程中作用

源码如下: package com.demo.app.api; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.Map; import org.apache.commons.httpclient.Cookie; import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; import

【ListViewJSON】【com.demo.app.common】【BitmapManager】源码分析及其在工程中作用

首先源码如下: package com.demo.app.common; import java.io.File; import java.io.IOException; import java.lang.ref.SoftReference; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.WeakHashMap; import java.util.con

【ListViewJSON】【com.demo.app.api】【JSONProvider】源码分析及其在工程中作用

源码如下: package com.demo.app.api; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.HashMap

【ListViewJSON】【com.demo.app.common】【StringUtils】源码分析及其在工程中作用

源码如下: package com.demo.app.common; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.regex.Pattern; import org.json.JSONArray; import org.json.JSONException; import

【ListViewJson】【com.demo.app】【AppConfig】源码分析及其在工程中作用

源码如下: package com.demo.app; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Properties; import android.content.Context; import android.content.SharedPreferences; import android.preference.Prefere

【ListViewJSON】【com.demo.app.common】【FileUtils】源码分析及其在工程中作用

源码如下: package com.demo.app.common; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import android.content.Context; imp

【ListViewJSON】【com.demo.app】【AppManager】源码分析及其在工程中作用

AppManager.java源码如下: package com.demo.app; import java.util.Stack; import android.app.Activity; import android.app.ActivityManager; import android.content.Context; /** * 应用程序Activity管理类:用于Activity管理和应用程序退出 * @version 1.0 * @created 2012-3-21 */ publi

【ListViewJSON】【com.demo.app.common】【ImageUtils】源码分析及其在工程中作用

首先源码: package com.demo.app.common; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.I