Android 独立线程上的同步HttpPost
我目前正在使用扩展AsyncTask的类将一些数据发布到php Web服务 现在我需要做一个同步调用,因为我想显示默认的异常弹出窗口,一旦数据发送到previousHandler.uncaughtExceptiont,e,我的应用程序就崩溃了 我可以在AsyncTask上禁用异步功能吗,或者您还有其他建议吗 这是我当前的代码,请提前感谢:Android 独立线程上的同步HttpPost,android,Android,我目前正在使用扩展AsyncTask的类将一些数据发布到php Web服务 现在我需要做一个同步调用,因为我想显示默认的异常弹出窗口,一旦数据发送到previousHandler.uncaughtExceptiont,e,我的应用程序就崩溃了 我可以在AsyncTask上禁用异步功能吗,或者您还有其他建议吗 这是我当前的代码,请提前感谢: public void uncaughtException(Thread t, Throwable e) { final Writer result
public void uncaughtException(Thread t, Throwable e) {
final Writer result = new StringWriter();
final PrintWriter printWriter = new PrintWriter(result);
e.printStackTrace(printWriter);
String stacktrace = result.toString();
printWriter.close();
String deviceUuid = Utilities.DeviceUuid(ctx, cr);
String bluetoothName = Utilities.LocalBluetoothName();
AsyncTasks.ErrorLogTask logTask = new AsyncTasks.ErrorLogTask(e, bluetoothName, deviceUuid, stacktrace);
logTask.execute();
//task wont be complete before this code below runs since its async.
previousHandler.uncaughtException(t, e);
}
我想你还是需要抓住这个例外。我会将调用保留在AsyncTask中,但捕获异常。使用标志作为doInBackground的返回值,或者创建AsyncTask的本地变量作为标志,并在onPostExecute中检查标志,必要时显示对话框 一般来说,由于ANR的风险,您不希望在主UI线程上进行服务器调用。请参阅这篇关于它们的文章:
将默认异常弹出窗口放入ErrorLogTask的onPostExecute方法中。当任务完成时,该块在主线程中执行