【发布时间】:2012-01-28 21:21:12
【问题描述】:
我想要一个通过 HTTP 请求加载或保存数据的应用程序,但是数据必须与 UI 线程交互。理想情况下,我希望单个线程在消息上使用 IF 语句来确定请求是“加载”还是“保存”。
用最少的代码做到这一点最简单的方法是什么?
此外,处理程序的实例是否在单个线程上运行?
编辑:这是我现在使用的代码:
Handler doStuff = new Handler(){
@Override
public void handleMessage(Message msg){
if(msg.what == 1){
// Load all the information.
// Get the ID from sharedPrefs
SharedPreferences details= getSharedPreferences("details", 0);
String ID = patDetails.getString("id", "error");
// Load up the ID from HTTP
String patInfo = httpInc.getURLContent("info.php?no="+AES.encrypt("387gk3hjbo8sgslksjho87s", ID));
// Separate all the details
patientInfo = patInfo.split("~");
}
if(msg.what == 2){
// Save the data
}
}
};
Eclipse 停止调试并显示 StrictMode.class 的“Source not found”
我想这是因为它使用主线程来访问互联网,尽管它是在单个线程中运行的。
任何想法。
【问题讨论】:
-
如果您发布您尝试过的代码以及任何错误消息会有所帮助。
标签: java android multithreading task