Android的获取字符串返回从AsyncTask的 [英] Android getting string back from AsyncTask
本文介绍了Android的获取字符串返回从AsyncTask的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有创建了以下类:
public class getURLData extends AsyncTask<String, Integer, String>{
@Override
protected String doInBackground(String... params) {
String line;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(params[0]);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
line = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
} catch (MalformedURLException e) {
line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
} catch (IOException e) {
line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
}
return line;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
}
}
我想调用它像这样:
I am trying to call it like so:
String output = null;
output = new getURLData().execute("http://www.domain.com/call.php?locationSearched=" + locationSearched);
但是,输出变量是没有得到数据,而不是我得到一个错误:
But the output variable isn't getting data, instead I am getting an error:
Type mismatch: cannot convert from AsyncTask<String,Integer,String> to String
请帮忙
推荐答案
方法执行
返回 AynscTask
本身,你需要调用 GET
:
The method execute
returns the AynscTask
itself, you need to call get
:
output =
new getURLData()
.execute("http://www.domain.com/call.php?locationSearched=" + locationSearched)
.get();
不过,如果你这样做,你只要把你的异步的任务成的同步的一个,因为 GET
等待如果需要的结果是即时拍摄
However, if you do that you just turned your async task into a sync one, as get
waits if needed for the result to be avilable.
参考:<一href="http://developer.android.com/reference/android/os/AsyncTask.html#get%28%29">AsyncTask.get
这篇关于Android的获取字符串返回从AsyncTask的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文