メイン スレッドで Web ページのコンテンツを読むとエラーが発生します
public class WebReader extends AsyncTask<String, String, String> {
private String result;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
String result;
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(params[0]);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
System.out.println(params[0]);
try {
result = client.execute(request, responseHandler);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.result = result;
return result;
}
protected void onProgressUpdate(String... progress) {
}
@Override
protected void onPostExecute(String result) {
this.result = result;
}
public String getResult(){
return result;
}
}
そして、私はこれを使用します:
public static void testAsync(){
String result = null;
WebReader wr = new WebReader();
wr.execute("http://www.google.fr");
result = wr.getResult();
while(result==null){
try {
Thread.sleep(1000);
result = wr.getResult();
} catch (InterruptedException e) {
result = "";
}
}
System.out.println(result);
}
文字列を返す解決策が本当に見つかりません
ありがとう