Context-detection API for Android developed as a university project
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ExceptionHandler.java 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package uk.co.md87.android.common;
  6. import java.io.IOException;
  7. import java.io.PrintWriter;
  8. import java.io.StringWriter;
  9. import java.io.Writer;
  10. import java.lang.Thread.UncaughtExceptionHandler;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. import org.apache.http.NameValuePair;
  14. import org.apache.http.client.entity.UrlEncodedFormEntity;
  15. import org.apache.http.client.methods.HttpPost;
  16. import org.apache.http.impl.client.DefaultHttpClient;
  17. import org.apache.http.message.BasicNameValuePair;
  18. import org.apache.http.protocol.HTTP;
  19. /**
  20. *
  21. * @author chris
  22. */
  23. public class ExceptionHandler implements UncaughtExceptionHandler {
  24. private UncaughtExceptionHandler defaultUEH;
  25. private String appname, url, version, imei;
  26. /*
  27. * if any of the parameters is null, the respective functionality
  28. * will not be used
  29. */
  30. public ExceptionHandler(String appname, String url, String version, String imei) {
  31. this.appname = appname;
  32. this.url = url;
  33. this.version = version;
  34. this.imei = imei;
  35. this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
  36. }
  37. public void uncaughtException(Thread t, Throwable e) {
  38. String timestamp = String.valueOf(System.currentTimeMillis());
  39. final Writer result = new StringWriter();
  40. final PrintWriter printWriter = new PrintWriter(result);
  41. e.printStackTrace(printWriter);
  42. String stacktrace = result.toString();
  43. printWriter.close();
  44. String filename = timestamp + ".stacktrace";
  45. sendToServer(stacktrace, filename);
  46. defaultUEH.uncaughtException(t, e);
  47. }
  48. private void sendToServer(String stacktrace, String filename) {
  49. DefaultHttpClient httpClient = new DefaultHttpClient();
  50. HttpPost httpPost = new HttpPost(url);
  51. httpPost.setHeader("x-application", appname + "-exception");
  52. httpPost.setHeader("x-version", version);
  53. httpPost.setHeader("x-imei", imei);
  54. List<NameValuePair> nvps = new ArrayList<NameValuePair>();
  55. nvps.add(new BasicNameValuePair("filename", filename));
  56. nvps.add(new BasicNameValuePair("stacktrace", stacktrace));
  57. try {
  58. httpPost.setEntity(
  59. new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
  60. httpClient.execute(httpPost);
  61. } catch (IOException e) {
  62. e.printStackTrace();
  63. }
  64. }
  65. }