Reputation: 2972
I want to retrieve the contents of this file and save that to a string.
I've tried using AsyncTask (based on this answer) and here is my class.
class RetreiveURLTask extends AsyncTask<Void, Void, String> {
private Exception exception = null;
public String ResultString = null;
protected String doInBackground(Void ... something) {
URL url;
try {
url = new URL("http://stream.lobant.net/ccfm.info");
HttpURLConnection urlConnection;
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
String stream_url = IOUtils.toString(in, "UTF-8");
urlConnection.disconnect();
return stream_url;
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (Exception e) {
this.exception = e;
return null;
}
}
protected void onPostExecute(String stream_url) {
// TODO: check this.exception
// TODO: do something with the feed
if (this.exception != null)
this.exception.printStackTrace();
this.ResultString = stream_url;
}
}
I've tried using my AsyncTask class like this:
AsyncTask<Void, Void, String> stream_task = new RetreiveURLTask().execute();
String stream_url = stream_task.ResultString;
but ResultString isn't recognised.
I'm confused about how this all works. Since the AsyncTask runs in the background, even if I could assign my string to one of the public variables, there is no guarentee that it will be valid when I make the assignment. Even if I were to use some kind of getResult() function, I would need to know when to call it so that the code has completed executing.
So, how is this usually done?
(Also, is my http read code ok?)
My ability: I can code, but am new to android.
Upvotes: 3
Views: 7515
Reputation: 1978
Make this
public String ResultString = "";
Global
Asynctask is not the main class here , that's the reason you get ResultString cannot be resolved or is not a field
make it a global variable then you'll be able to access it
Upvotes: 1
Reputation: 133560
Use an interface
new RetreiveURLTask(ActivityName.this).execute();
In your asyctask
TheInterface listener;
In the constructor
public RetreiveURLTask (Context context)
{
listener = (TheInterface) context;
}
The interface
public interface TheInterface {
public void theMethod(String result);
}
In onPostExecute
if (listener != null)
{
listener.theMethod(stream_url);
}
In your activity class implement the interface
implements RetreiveURLTask.TheInterface
Implement the method
@Override
public void theMethod(String result) {
// update ui using result
}
Upvotes: 9
Reputation: 71
The execution of the AsyncTask is started in the background, while the assignment is carried out immediately after the execution began. Therefore, the assignment is carried out before the execution of the AsyncTask is complete.
Your best bet is to conduct the assignment in the OnPostExecute() method.
Upvotes: 0
Reputation: 24848
// try this way
RetreiveURLTask task = new RetreiveURLTask();
task.execute();
private void response(String responseData){
// here you write your code which use responseData
}
class RetreiveURLTask extends AsyncTask<Void, Void, String> {
private Exception exception = null;
public String ResultString = null;
protected String doInBackground(Void ... something) {
URL url;
try {
url = new URL("http://stream.lobant.net/ccfm.info");
HttpURLConnection urlConnection;
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
String stream_url = IOUtils.toString(in, "UTF-8");
urlConnection.disconnect();
return stream_url;
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (Exception e) {
this.exception = e;
return null;
}
}
protected void onPostExecute(String stream_url) {
// TODO: check this.exception
// TODO: check this.exception
// TODO: do something with the feed
super.onPostExecute(stream_url);
response(stream_url)
}
}
Upvotes: 4