1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
-
- package uk.co.md87.android.common;
-
- 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.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;
-
- /**
- *
- * @author chris
- */
- public class ExceptionHandler implements UncaughtExceptionHandler {
-
- private UncaughtExceptionHandler defaultUEH;
-
- private String appname, url, version, imei;
-
- /*
- * if any of the parameters is null, the respective functionality
- * will not be used
- */
- public ExceptionHandler(String appname, String url, String version, String imei) {
- this.appname = appname;
- this.url = url;
- this.version = version;
- this.imei = imei;
- this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
- }
-
- public void uncaughtException(Thread t, Throwable e) {
- String timestamp = String.valueOf(System.currentTimeMillis());
- final Writer result = new StringWriter();
- final PrintWriter printWriter = new PrintWriter(result);
- e.printStackTrace(printWriter);
- String stacktrace = result.toString();
- printWriter.close();
- String filename = timestamp + ".stacktrace";
-
- sendToServer(stacktrace, filename);
-
- defaultUEH.uncaughtException(t, e);
- }
-
- private void sendToServer(String stacktrace, String filename) {
- DefaultHttpClient httpClient = new DefaultHttpClient();
- HttpPost httpPost = new HttpPost(url);
-
- httpPost.setHeader("x-application", appname + "-exception");
- httpPost.setHeader("x-version", version);
- httpPost.setHeader("x-imei", imei);
-
- List<NameValuePair> nvps = new ArrayList<NameValuePair>();
- nvps.add(new BasicNameValuePair("filename", filename));
- nvps.add(new BasicNameValuePair("stacktrace", stacktrace));
- try {
- httpPost.setEntity(
- new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
- httpClient.execute(httpPost);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
|