I'm using this method to read an html page, and it's making a Toast with the result. But I wanted it to return the read value, or put it in some variable, but I did not succeed.
private class GrabURL extends AsyncTask<String, Void, Void> {
private final HttpClient Client = new DefaultHttpClient();
private String Content;
private String Error = null;
private ProgressDialog Dialog = new ProgressDialog(getActivity());
protected void onPreExecute() {
Dialog.setMessage("Carregando. Aguarde...");
Dialog.show();
}
protected Void doInBackground(String... urls) {
try {
HttpGet httpget = new HttpGet(urls[0]);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
Content = Client.execute(httpget, responseHandler);
} catch (ClientProtocolException e) {
Error = e.getMessage();
cancel(true);
} catch (IOException e) {
Error = e.getMessage();
cancel(true);
}
return null;
}
protected void onPostExecute(Void unused) {
Dialog.dismiss();
if (Error != null) {
Toast.makeText(getActivity(), Error, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getActivity(), "Source: " + Content, Toast.LENGTH_LONG).show();
retorno = Content; // Essa variavel retorno é onde queria setar.
}
}
}