AsyncTask的 - 都需要扩大和doInBackground什么参数? [英] AsyncTask - what parameters are needed for extending and doInBackground?
问题描述
什么是不对的,它使用的AsyncTask code?尤其是: - 我需要把fetchSchools什么参数 - 我需要什么参数放在doInBackground?
What is wrong with this code that uses AsyncTask? In particular: - what parameters do I need to put in fetchSchools - what parameters do I need to put in doInBackground?
我发现很多有用的例子,但它们都使用伪code。在这些参数却没有解释什么,我确实需要放在那里。
I've found lots of "helpful" examples but they all use pseudocode in these parameters and don't explain what I actually need to put there.
我得到了Eclipse错误的方法fetchSchools必须实现继承的抽象方法AsynchTask ......
"I get the Eclipse error the method fetchSchools must implement the inherited abstract method AsynchTask..."
我并不需要通过这样的东西,我希望它返回一个字符串。
I don't need to pass this anything, I expect it to return a string.
public class fetchSchools extends AsyncTask<Void, Void, String> {
public String doInBackground(String retval) {
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://www.domain/schools.php");
try
{
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String
line;
int a=0;
while ((line = reader.readLine()) != null) {
builder.append(line);
Log.i(MainActivity.class.getName(), "Reading in: " + a +" : "+ line);
a++;
}
} else {
Log.e(MainActivity.class.toString(), "Failed to download file");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
return builder.toString();
}
protected void onPostExecute() {
}
}
推荐答案
你给doInBackground字符串参数,以便异步任务的第一个参数必须是字符串不为空。
you gave doInBackground a string parameter so async task first parameter must be string not void.
AsyncTask<String , Void, String> {
如果你不希望传递一个参数,不以doInBackground函数给出的参数。
If you don't want to pass a parameter, don't give parameter to doInBackground function.
浏览此网页AsyncTask的参考: <一href="http://developer.android.com/reference/android/os/AsyncTask.html">http://developer.android.com/reference/android/os/AsyncTask.html
check this page for asynctask reference: http://developer.android.com/reference/android/os/AsyncTask.html
的AsyncTask的第一个参数去doInBackground功能,第二个去onprogressUpdate功能,第三个参数尽皆onpostexecute功能。
First parameter of asynctask goes to doInBackground function, second one goes to onprogressUpdate function, third parameter foes to onpostexecute function.
我觉得要做到这一点:
public class fetchSchools extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... arg0) {
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
// ...
return builder.toString();
}
protected void onPostExecute(String retval)
{
}
}
这篇关于AsyncTask的 - 都需要扩大和doInBackground什么参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!