private StringgetStackTrace(Throwable th) {
final Writer result =new StringWriter();
final PrintWriter printWriter =new PrintWriter(result);
// If the exception was thrown in a background thread inside
// AsyncTask, then the actual exception can be found with getCause
Throwable cause = th;
while (cause !=null) {
cause.printStackTrace(printWriter);
cause = cause.getCause();
}
final String stacktraceAsString = result.toString();
printWriter.close();
return stacktraceAsString;
}