AsyncTask を使用するこのコードの問題点は何ですか? 特に: - fetchSchools に入力する必要があるパラメーターは何ですか? - doInBackground に入力する必要があるパラメーターは何ですか?
多くの「役立つ」例を見つけましたが、それらはすべてこれらのパラメーターで疑似コードを使用しており、実際にそこに何を入れる必要があるかを説明していません。
「メソッド fetchSchools は継承された抽象メソッド AsynchTask を実装する必要があるという Eclipse エラーが発生します...」
これには何も渡す必要はありません。文字列が返されることを期待しています。
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() {
}
}