Android活动太忙,无法设置文本视图文本?
我有一个函数,可以用http查询的条目填充SQLite数据库:Android活动太忙,无法设置文本视图文本?,android,android-activity,settext,Android,Android Activity,Settext,我有一个函数,可以用http查询的条目填充SQLite数据库: try { stringEntity = new StringEntity(SQL); httpPost.setEntity(stringEntity); httpResponse = httpClient.execute(httpPost); httpEntity = httpResponse.getEntity(); bos = new ByteAr
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();
}
我试图做的是在过程开始之前(在我的代码i poset中的第一个“try{..”前面)设置活动的textview文本。
但是文本不会改变,因为我的活动太忙了,无法获取数据(我想,我没有其他解释……)
有什么建议吗
谢谢,
prexx
更新
“”从异步任务“”获取数据
txtAction.setText(“加载…”);
AsyncTask test=new cAsyncTask();
试一试{
data=test.execute(URL.get();
读卡器=新的BufferedReader(
新的StringReader(数据);
而((line=reader.readLine())!=null){
//在DB | | WORKS中保存数据
}
}
}捕获(IOE异常){
e、 printStackTrace();
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(执行例外){
//TODO自动生成的捕捉块
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;
}
类cAsyncTask扩展异步任务{
受保护的字符串doInBackground(字符串…URL){
int count=url.length;
字符串数据=”;
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpPost-HttpPost;
字符串实体字符串实体;
HttpResponse HttpResponse;
HttpEntity HttpEntity;
ByteArrayOutputStream bos;
弦线;
缓冲读取器;
for(int i=0;itextview.setText(“foo”)
之后调用AsyncTask.execute()
,您就可以了:)
问候
使用代码示例更新:
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!");
}
}
txtAction.setText(“加载…”);
AsyncTask test=new cAsyncTask();
测试。执行(“http://...");
类cAsyncTask扩展了异步任务{
受保护的字符串doInBackground(字符串…URL){
int count=url.length;
字符串数据=”;
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpPost-HttpPost;
字符串实体字符串实体;
HttpResponse HttpResponse;
HttpEntity HttpEntity;
ByteArrayOutputStream bos;
弦线;
缓冲读取器;
for(int i=0;i
关键是将所有繁忙的代码放入将在一个单独线程中运行的
doInBackGround
方法。所有UI修改必须在同一个UI线程中,这可以在将在同一UI线程中执行的onPostExecute
方法中完成。将代码的繁忙部分放入一个单独的线程中
看看这个实用程序
在textview.setText(“foo”)
之后调用AsyncTask.execute()
,您就可以了:)
问候
使用代码示例更新:
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!");
}
}
txtAction.setText(“加载…”);
AsyncTask test=new cAsyncTask();
测试。执行(“http://...");
类cAsyncTask扩展了异步任务{
受保护的字符串doInBackground(字符串…URL){
int count=url.length;
字符串数据=”;
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpPost-HttpPost;
字符串实体字符串实体;
HttpResponse HttpResponse;
HttpEntity HttpEntity;
ByteArrayOutputStream bos;
弦线;
缓冲读取器;
for(int i=0;i
关键是将所有繁忙的代码放入doInBackGround
方法中,该方法将在单独的线程中运行。所有UI修改必须在同一UI线程中,这可以在