public class CustomExceptionHandler implements UncaughtExceptionHandler {
private UncaughtExceptionHandler defaultUEH;
/*
* if any of the parameters is null, the respective functionality
* will not be used
*/
public CustomExceptionHandler
(String localPath,
String url
) { this.localPath = localPath;
this.url = url;
this.
defaultUEH = Thread.
getDefaultUncaughtExceptionHandler(); }
String timestamp
= TimestampFormatter.
getInstance().
getTimestamp(); e.printStackTrace(printWriter);
String stacktrace
= result.
toString(); printWriter.close();
String filename
= timestamp
+ ".stacktrace";
if (localPath != null) {
writeToFile(stacktrace, filename);
}
if (url != null) {
sendToServer(stacktrace, filename);
}
defaultUEH.uncaughtException(t, e);
}
private void writeToFile
(String stacktrace,
String filename
) { try {
localPath + "/" + filename));
bos.write(stacktrace);
bos.flush();
bos.close();
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("stacktrace", stacktrace));
try {
httpPost.setEntity(
new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
httpClient.execute(httpPost);
e.printStackTrace();
}
}
}