我有一个功能,用http查询的条目填充我的SQLite DB:
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的文本(在我的代码i postet中的第一个“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;
}
答案 0 :(得分:4)
将代码的繁忙部分放入一个单独的线程中。
查看AsyncTask实用程序
在AsyncTask.execute()
之后立即致电textview.setText("foo")
,你会没事的。)
此致
使用代码示例进行更新:
txtAction.setText("Loading...");
AsyncTask<String, String, String> test = new cAsyncTask();
test.execute("http://...");
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();
}
}
reader = new BufferedReader(
new StringReader(data));
String line = "";
while ((line = reader.readLine()) != null) {
//SAVE DATA IN DB || WORKS
}
return data;
}
protected void onPostExecute(String result) {
String test = result;
textView.setText("Done!");
}
}
关键是将所有忙代码放入doInBackGround
方法中,该方法将在一个单独的线程中运行。所有UI修改必须在同一个UI线程中,这可以在onPostExecute
方法中完成,该方法将在同一个UI线程中执行
答案 1 :(得分:0)
您可以尝试在TextView上调用invalidate()。但是使用异步任务是重型数据加载方法的最佳实践。这不会在操作UI控件中中断用户。
答案 2 :(得分:0)
它与“太忙”无关,但事实是文本只会在方法返回时设置。随着你的网络,这将被推迟。
顺便说一下。在UI线程上的Honeycomb网络上将抛出异常并终止你的应用程序。