Android的异步后块UI线程 [英] Android asynchronous post blocks UI thread
本文介绍了Android的异步后块UI线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我开发一个Android应用程序。我要发布使用AsyncTask的一个服务器。不过,我仍然有这表明该UI线程被阻塞错误。
我要解析XML响应,并在列表视图中显示,但我无法继续,因为UI线程仍然受阻。
公共类AsynchronousPost扩展ListActivity实现OnClickListener {的EditText SearchValue;
按钮SearchBtn;
字符串URL =;@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.search_interface);
SearchBtn =(按钮)findViewById(R.id.searchbtn);
SearchBtn.setOnClickListener(本);
}公共无效的onClick(视图查看){ 新MyAsyncTask()执行();
}私有类MyAsyncTask扩展的AsyncTask<字符串,整数,文档> { 私人最终字符串URL =URL; 私人最终字符串username =用户名;
私人最终字符串密码=密码;
私人的EditText SearchValue;
@覆盖
受保护的文档doInBackground(字符串...为arg0){
// TODO自动生成方法存根 getXmlFromUrl(URL); //获取XML
返回null;
} @覆盖
保护无效onPostExecute(){ //要解析XML响应
//显示列表视图
}
公共字符串getXmlFromUrl(字符串URL){
字符串XML = NULL; 尝试{
// defaultHttpClient
DefaultHttpClient的HttpClient =新DefaultHttpClient();
HttpPost httpPost =新HttpPost(URL);
SearchValue =(EditText上)findViewById(R.id.search_item);
字符串Schvalue = SearchValue.getText()的toString()。 //添加数据
清单<&的NameValuePair GT; namevaluepairs中=新的ArrayList<&的NameValuePair GT;(
5);
namevaluepairs中
。新增(新BasicNameValuePair(用户名的用户名));
namevaluepairs中
。新增(新BasicNameValuePair(密码,密码));
nameValuePairs.add(新BasicNameValuePair(searchItem
Schvalue)); //响应存储在响应VAR
HTT presponse HTT presponse = httpClient.execute(httpPost);
HttpEntity httpEntity = HTT presponse.getEntity();
XML = EntityUtils.toString(httpEntity); }赶上(UnsupportedEncodingException五){
e.printStackTrace();
返回null;
}赶上(ClientProtocolException E){
e.printStackTrace();
返回null;
}赶上(IOException异常五){
e.printStackTrace();
返回null;
}
//返回XML
返回XML;
}
}}
解决方案
受保护对象doInBackground(字符串... PARAMS){
// AsyncTask的这种方法是不是在UI线程中运行 - 在这里只做非UI TAKS
返回结果;
}@覆盖
保护无效onPostExecute(对象结果){
//我不知道,但我认为这种方法是在UI线程运行
//如果你有很长的操作这里你会阻塞UI线程
//放在doInBackground的任务...
//填写UI元素使用元素
//
runOnUiThread(新的Runnable(){
@覆盖
公共无效的run(){
//在这里填写您的UI元素 }});
}
I'm developing an Android app. I want to post to a server using asynctask. However, I still have an error which indicates that the UI thread is blocked.
I want to parse the XML response and display it in a list view, but I cannot proceed because the UI thread is still blocked.
public class AsynchronousPost extends ListActivity implements OnClickListener {
EditText SearchValue;
Button SearchBtn;
String URL = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_interface);
SearchBtn = (Button) findViewById(R.id.searchbtn);
SearchBtn.setOnClickListener(this);
}
public void onClick(View views) {
new MyAsyncTask().execute();
}
private class MyAsyncTask extends AsyncTask<String, Integer, Document> {
private final String URL = "url";
private final String username = "username";
private final String password = "password";
private EditText SearchValue;
@Override
protected Document doInBackground(String... arg0) {
// TODO Auto-generated method stub
getXmlFromUrl(URL); // getting XML
return null;
}
@Override
protected void onPostExecute() {
//want to parse xml response
//display on listview
}
public String getXmlFromUrl(String url) {
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
SearchValue = (EditText) findViewById(R.id.search_item);
String Schvalue = SearchValue.getText().toString();
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
5);
nameValuePairs
.add(new BasicNameValuePair("username", username));
nameValuePairs
.add(new BasicNameValuePair("password", password));
nameValuePairs.add(new BasicNameValuePair("searchItem",
Schvalue));
// response stored in response var
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
} catch (ClientProtocolException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
// return XML
return xml;
}
}
}
解决方案
protected Object doInBackground(String... params) {
//this method of AsyncTask is not running on the UI Thread -- here do just non UI taks
return result;
}
@Override
protected void onPostExecute(Object result) {
//I'm not sure but I think this method is running on the UI Thread
//If you have long operations here to do you will block UI Thread
//put the tasks in the doInBackground...
//to fill the elements in the UI elements use
//
runOnUiThread (new Runnable() {
@Override
public void run() {
//here fill your UI elements
}});
}
这篇关于Android的异步后块UI线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文