这段使用 AsyncTask 的代码有什么问题?特别是: - 我需要在 fetchSchools 中放入哪些参数 - 我需要在 doInBackground 中放入哪些参数?
我发现了很多“有用”的示例,但它们都在这些参数中使用伪代码,并且没有解释我实际需要放在那里的内容。
“我得到 Eclipse 错误,方法 fetchSchools 必须实现继承的抽象方法 AsynchTask...”
我不需要传递任何东西,我希望它返回一个字符串。
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() {
}
}