Created
November 23, 2012 03:59
-
-
Save showsky/4133956 to your computer and use it in GitHub Desktop.
Diy Android sentry
1. report use http post
2. save log ont the sdcard storage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.android; | |
import android.app.Application; | |
public class App extends Application { | |
public App() { | |
Thread.setDefaultUncaughtExceptionHandler(new SentryLog("/sdcard/tmp", "http//www.miiicasa.com")); | |
} | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.android; | |
import android.util.Log; | |
public class Logger { | |
private final static String TAG = "android_test"; | |
private static String _message(String message, String tag) { | |
return "[" + tag + "] " + message; | |
} | |
public static void v(String message, String tag) { | |
Log.v(TAG, _message(message, tag)); | |
} | |
public static void e(String message, String tag) { | |
Log.e(TAG, _message(message, tag)); | |
} | |
public static void i(String message, String tag) { | |
Log.i(TAG, _message(message, tag)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.android; | |
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.Calendar; | |
import org.apache.http.client.methods.HttpPost; | |
import org.apache.http.entity.mime.HttpMultipartMode; | |
import org.apache.http.entity.mime.MultipartEntity; | |
import org.apache.http.entity.mime.content.FileBody; | |
import android.net.http.AndroidHttpClient; | |
public class SentryLog implements UncaughtExceptionHandler { | |
private UncaughtExceptionHandler defaultUEH; | |
private String localPath; | |
private String url; | |
public SentryLog(String localPath, String url) { | |
this.localPath = localPath; | |
this.url = url; | |
this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler(); | |
} | |
@Override | |
public void uncaughtException(Thread t, Throwable e) { | |
String timestamp = String.valueOf(Calendar.getInstance().getTimeInMillis()); | |
final Writer result = new StringWriter(); | |
final PrintWriter printWriter = new PrintWriter(result); | |
e.printStackTrace(printWriter); | |
String stacktrace = result.toString(); | |
printWriter.close(); | |
String filename = timestamp + ".stacktrace"; | |
File file = null; | |
if (localPath != null) { | |
file = writeToFile(stacktrace, filename); | |
} | |
if (url != null) { | |
sendToServer(stacktrace, file); | |
} | |
defaultUEH.uncaughtException(t, e); | |
} | |
private File writeToFile(String stacktrace, String filename) { | |
File dir = new File(filename); | |
if ( ! dir.exists()) dir.mkdirs(); | |
String savePath = localPath + "/" + filename; | |
File file = new File(savePath); | |
try { | |
FileWriter fw = new FileWriter(file); | |
BufferedWriter bos = new BufferedWriter(fw); | |
bos.write(stacktrace); | |
bos.flush(); | |
bos.close(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return file; | |
} | |
private void sendToServer(String stacktrace, File filename) { | |
AndroidHttpClient httpClient = AndroidHttpClient.newInstance("Android"); | |
HttpPost httpPost = new HttpPost(url); | |
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); | |
entity.addPart(filename.getName(), new FileBody(filename)); | |
try { | |
httpClient.execute(httpPost); | |
entity.consumeContent(); | |
} catch (IOException e) { | |
httpPost.abort(); | |
e.printStackTrace(); | |
} | |
httpClient.getConnectionManager().shutdown(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment