我是创建的AsyncTask,retreive字符串数据:
i am create asynctask, retreive string data:
View.OnClickListener btnClk = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (btnGetData.getId() == ((Button) v).getId()) {
Object obj = new FetchTask(Main.this).execute((Integer) null);
}
}
};
我的任务:
public class FetchTask extends AsyncTask<Object, Object, Object> {
private Context ctx;
private String xml;
public FetchTask(Context context) {
ctx = context;
}
@Override
protected void onPreExecute() {
DialogDownload_Ring = ProgressDialog.show(Main.this, "Download...",
"Fetching XML data...");
DialogDownload_Ring.setCancelable(true);
DialogDownload_Ring.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub
cancel(true);
}
});
super.onPreExecute();
}
@Override
protected void onCancelled() {
super.onCancelled();
}
@Override
protected void onPostExecute(Object result) {
if (result instanceof Exception) {
DialogDownload_Ring.setMessage("Download failed!");
} else {
DialogDownload_Ring.setMessage("Succesfully Downloaded!");
}
DialogDownload_Ring.dismiss();
super.onPostExecute(result);
}
@Override
protected Object doInBackground(Object... params) {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(URL_Local);
HttpResponse httpResponce = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponce.getEntity();
xml = EntityUtils.toString(httpEntity);
return xml;
} catch (Exception e) {
return e;
}
}
}
如何从obj对象retreive的XML数据? obj.toString()不工作。别的我创建的AsyncTask字符串属性(延伸AsyncTask的字符串,字符串,字符串)它的工作,但之后retreive XML数据的进度开始,我也想不明白。请帮帮我。附:对不起,我的英语不是很好。
How to retreive xml data from Object obj? obj.toString() not working. Else i create asynctask string properties (extends AsyncTask String, String, String) it's working but progressbar started after retreive xml data, i also do not understand. Help me please. P.S. Sorry my English not good..
如果你想在 doInBackground
来得到一个结果回到主UI线程执行完毕,那么你将需要通过调用 AsyncTask.get()的方法,但这样会直到doInBackground执行完成冻结UI线程。您将需要调用 AsyncTask.get()
A线程内以避免UI冻结。首页的AsyncTask
才能得到的结果早在UI线程:
If you want to get a result back in the main ui thread after doInBackground
execution complete then you will need to execute AsyncTask by calling AsyncTask.get() method but this will freeze the UI thread until doInBackground execution is complete. You will need to call AsyncTask.get()
inside a Thread to avoid a UI freeze. Start AsyncTask
as to get the result back in the UI Thread:
Object obj = new FetchTask(Main.this).execute((Integer) null).get();
第二种方式是启动的AsyncTask,而无需调用 AsyncTask.get
方法和接收结果 onPostExecute
方法时 doInBackground
执行完成。开始的AsyncTask作为按钮点击:
A Second way is start AsyncTask without calling AsyncTask.get
method and receive the result in onPostExecute
method when doInBackground
execution is complete.
start AsyncTask as on button click:
new FetchTask(Main.this).execute((Integer) null);
获取导致 onPostExecute
为:
@Override
protected void onPostExecute(Object result) {
// extract xml from result
super.onPostExecute(result);
}
上一篇:得到Play商店中的应用程序默认电子邮件地址应用程序、电子邮件地址、商店、Play
下一篇:获取生成错误创建使用Xamarin扩展的Visual Studio 2015年preVIEW Android应用程序应用程序、错误、Visual、Xamarin