Android活动太忙而无法设置TextView文本?
|
我有一个函数用http查询的条目填充我的SQLite数据库:
try {
stringEntity = new StringEntity(SQL);
httpPost.setEntity(stringEntity);
httpResponse = httpClient.execute(httpPost);
httpEntity = httpResponse.getEntity();
bos = new ByteArrayOutputStream();
httpEntity.writeTo(bos);
data = bos.toString();
reader = new BufferedReader(
new StringReader(data));
try {
//SAVE DATA IN MY DB || WORKS
} catch(IOException e) {
e.printStackTrace();
}
} catch (IOException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
我想做的是在过程开始之前在我的活动上设置textview的文本(在我发布的代码中的第一个\“ try {.. \”前面)。
但文字不会更改,因为我的活动太忙而无法获取数据(我想。我没有其他解释。)
有什么建议么?
谢谢,
prexx
更新
\'\'从AsyncTask获取数据\'\'
txtAction.setText(\"Loading...\");
AsyncTask<String, String, String> test = new cAsyncTask();
try {
data = test.execute(URL).get();
reader = new BufferedReader(
new StringReader(data));
while ((line = reader.readLine()) != null) {
//SAVE DATA IN DB || WORKS
}
}
} catch(IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我的异步任务:
class cAsyncTask extends AsyncTask<String, String, String> {
protected String doInBackground(String... urls) {
int count = urls.length;
String data = \"\";
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost;
StringEntity stringEntity;
HttpResponse httpResponse;
HttpEntity httpEntity;
ByteArrayOutputStream bos;
String line;
BufferedReader reader;
for (int i = 0; i < count; i++) {
httpPost = new HttpPost(urls[i].toString());
try {
stringEntity = new StringEntity(SQL);
httpPost.setEntity(stringEntity);
httpResponse = httpClient.execute(httpPost);
httpEntity = httpResponse.getEntity();
bos = new ByteArrayOutputStream();
httpEntity.writeTo(bos);
data = bos.toString();
} catch (IOException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
}
return data;
}
protected void onProgressUpdate(String... progress) {
}
protected void onPostExecute(String result) {
String test = result;
}
没有找到相关结果
已邀请:
3 个回复
良阑纠苫
之后再致电
,您可以:) 问候 使用代码示例进行更新:
关键是将所有繁忙代码放入
方法中,该方法将在单独的线程中运行。所有UI修改都必须在同一UI线程中,并且可以在将在同一UI线程中执行的
方法中完成
漂汀拦
僻朵庙惩竣