CustomExceptionHandler implements UncaughtExceptionHandler : UncaughtExceptionHandler « Development « Android






CustomExceptionHandler implements UncaughtExceptionHandler

    
//package com.jleorz.common;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import android.content.Context;

public class CustomExceptionHandler implements UncaughtExceptionHandler {

  private UncaughtExceptionHandler defaultUEH;

  private String localPath;

  private String url;

  private String appName;

  private Context activity;


  public static final String VERSION = "VERSION";
  public static final String LOCALPATH = "LOCALPATH";
  public static final String SENDERRORURL = "URL";
  public static final String ERROREEQUESR = "ERROREEQUESR";
  public static final String APP_NAME = "APP_NAME";

  public CustomExceptionHandler(Context activity) {
    this.localPath = LOCALPATH;
    this.url = SENDERRORURL;
    this.appName = APP_NAME;
    this.activity = activity;
    this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
  }

  public void uncaughtException(Thread t, Throwable e) {
    final Writer result = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(result);
    e.printStackTrace(printWriter);
    String stacktrace = result.toString();
    printWriter.close();
    String filename = new Date().getTime() + ".stacktrace";

    if (localPath != null) {
      File file = new File(localPath);
      if (!file.exists())
        try {
          file.createNewFile();
        } catch (IOException e1) {

        }
      writeToFile(stacktrace, filename);
    }

    if (url != null) {
      sendToServer(stacktrace, filename);
    }

    defaultUEH.uncaughtException(t, e);
  }

  private void writeToFile(String stacktrace, String filename) {
    try {
      BufferedWriter bos = new BufferedWriter(new FileWriter(localPath
          + "/" + filename));
      bos.write(stacktrace);
      bos.flush();
      bos.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  private void sendToServer(String stacktrace, String filename) {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);
    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    nvps.add(new BasicNameValuePair("filename", filename));
    nvps.add(new BasicNameValuePair("appName", appName));
    nvps.add(new BasicNameValuePair("stacktrace", stacktrace));
    nvps.add(new BasicNameValuePair("deviceInfo", android.os.Build.MODEL
        + "," + android.os.Build.VERSION.SDK + ","
        + android.os.Build.VERSION.RELEASE));
    nvps.add(new BasicNameValuePair("versionCode", "1"));

    try {
      httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
      httpClient.execute(httpPost);
    } catch (IOException e) {
      //
    }
  }
}

   
    
    
    
  








Related examples in the same category

1.implements UncaughtExceptionHandler
2.Error Reporter