Passing a string from a AsyncTask to another activity - android

I'm trying to use the HTTPClient to get the html code of a website using an AsyncTask and passing back the code to my main Activity.
I used this code in order to (try to) do that :
public class AsyncTaskGet extends AsyncTask<String, String, String>{
String result;
InputStream in;
protected String doInBackground(String... params) {
try
{
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpRequest = new HttpGet("http://www.google.fr");
HttpResponse response = httpClient.execute(httpRequest);
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(null, result), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
if (response != null) {
in = response.getEntity().getContent();
}
in.close();
result = sb.toString();
}catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
}catch(IOException e){
e.printStackTrace();
}
return result;
}
#Override
protected void onPostExecute(String result) {
//Here is one of my problem, I don't know how to get the returned value result as a string.
}
}
This is my AsyncTaskGet, since I tried lots of fruitless attempts, I erased all my code in the main activity except this part.
new AsyncTaskGet().execute();
So basically, What i'd like to do is getting the result back on my main activity as a string to display it in a TextView.
For now I want my app to be simple so I can understand the basics of using AsyncTasks, I didn't check for internet conectivity or stuff like that, I add the permission on the manifest to use internet (obviously
(Please be a little indulgent since it is one of my first apps)
One last thing, please excuse my poor english, i'm unfortunately not a native english speaker as you must have seen.
In advance, thanks a lot !
Sincerely :)

The easiest thing is to pass the calling activity via a constructor to your AsyncTask and create a method that will take the result as a parameter in the activity itself.
private Activity mActivity;
public AsyncTaskGet(Activity activity) {
mActivity = activity;
}
protected void onPostExecute(String result) {
mActivity.takeItBack(result);
}
There are some limitations: the current code just works with a 1 to 1 relationship between AsyncTask and Activity, means the private member variable type must be the activity from which you call it. If you don't want that you need to implement an interface:
// interface
public interface AsyncTaskCallback {
void takeItBack(String result);
}
// activity
public MyActivity implements AsyncTaskCallback {
#Override
public void takeItBack(String result) {
// do something
}
}
// AsyncTask
public class AsyncTaskGet extends AsyncTask<String, String, String> {
private AsyncTaskCallback mCallback;
public AsyncTaskGet(AsyncTaskCallback callback) {
mCallback= callback;
}
protected void onPostExecute(String result) {
mCallback.takeItBack(result);
}
}

You are already passing result to onPostExecute() as a String. If your AsyncTask is an inner class of your Activity then you can set result to a field variable in your Activity. Otherwise, you can call a function in your Activity from onPostExecute() to do what you want with the data.
Also, your poor English is really not bad at all :)

Related

Finish procedure before starting a new one

I am beginner in Android and I need some help. So, I have a procedure with sub-procedures inside. How can I finish one before starting a new one. Here is a code to better understand:
public void onCellLocationChanged(CellLocation lokacija) {
super.onCellLocationChanged(lokacija);
location = (GsmCellLocation) Phone.getCellLocation();
textCellId.setText(String.valueOf(location.getCid() % 65536));
textCellLac.setText(String.valueOf(location.getLac()));
String JSON_URL_string=JSON_URL + "?cellid=" + String.valueOf(location.getCid()%65636);
getJSON(JSON_URL_string);
myJSONString = textCellNameSakriven.getText().toString();
ParseJSON(myJSONString);
}
Problem is that myJSONString is empty, cause textCEllNameSkriven is also empty. That textView textCellNameSkriven is made when getJSON(JSON_URL_string) is finished. If I run debugger and go step by step, app goes directly from getJSON(JSON_URL_string) row to the next one and the next etc
Edit: Maybe the problem is that onPostExecute is not finished before starting ParseJSON. Here is also a code for getJSON:
private void getJSON(String url) {
class GetJSON extends AsyncTask<String, Void, String> {
ProgressDialog loading;
#Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(MainActivity.this, "Please Wait...", null, true, true);
}
#Override
protected String doInBackground(String... params) {
String uri = params[0];
BufferedReader bufferedReader = null;
try {
URL url = new URL(uri);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
StringBuilder sb = new StringBuilder();
bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String json;
while ((json = bufferedReader.readLine()) != null) {
sb.append(json + "\n");
}
return sb.toString().trim();
} catch (Exception e) {
return null;
}
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
textCellNameSakriven.setText(s);
}
}
GetJSON gj = new GetJSON();
gj.execute(url);
}
Since you are running a async task it practically runs on a different thread, so your getJson method need not wait for post execute and can return after starting the async task, so you can never be sure in this way that parseJson gets executed after textView is populated. You are running into classic race condition issue.
Your issue could be easily solved, if you have a callback which is called after postExecute is done, and you can handle parseJson there
So, something simple like , create interface MyCallback
public interface MyCallback {
public OnReadJsonDone();
}
Let your activity implement this MyCallback
public MainActivity implements MyCallback
{
...........
#Override
public OnReadJsonDone(){
parseJson();
}
Now change signature of getJson to
getJSON(string json, final MyCallback callback) {
Now in onpostexecute
//call OnReadJsonDone
callback.OnReadJsonDone()
So, all you now need is while calling getJSon pass this as second param
getJSON(JSON_URL_string,this);
Haven't tested this but you get the idea

Stop/Resume activities and onCreate in Android

I'm a new Android developer and I have a question. After users login, I need to get some data from an external URL and display them on one of my activities. But I've been reading and found this:
When your activity comes back to the foreground from the stopped state, it receives a call to onRestart(). The system also calls the
onStart() method, which happens every time your activity becomes
visible (whether being restarted or created for the first time).
And this
Caution: Your activity will be destroyed and recreated each time the
user rotates the screen. When the screen changes orientation, the
system destroys and recreates the foreground activity because the
screen configuration has changed and your activity might need to load
alternative resources (such as the layout).
Here and here.
So, it looks like I should not get the data I need on the onCreate method. Then where? In a previous activity and saving the data in the phone memory? That doesn't sound good to me.
Thanks in advance.
edit
I'm using AsyncTask as suggested, but everytime I switch the phone orientation, onCreate method is called.
My MainActivity:
public class MainActivity extends Activity implements AsyncResponse {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ReadJSONTask jsonTask = new ReadJSONTask();
jsonTask.delegate = this;
jsonTask.execute("http://www.myweb.com/myscript.php");
}
#Override
public void processFinish(String output) {
Toast.makeText(getApplicationContext(), output, Toast.LENGTH_LONG).show();
}
}
ReadJSONTask class:
public class ReadJSONTask extends AsyncTask<String, Void, String> {
public AsyncResponse delegate = null;
public String res;
public Boolean finish = false;
#Override
protected String doInBackground(String... url) {
String response = null;
String adres = url[0];
URL url_HTTPRequest = null;
try {
url_HTTPRequest = new URL(adres);
response = transfer(url_HTTPRequest);
} catch (MalformedURLException e) {
Log.e("URL ERROR", "MalformedURLException");
} catch (Exception e) {
Log.e("URL ERROR", "exc");
}
return response;
}
#Override
protected void onPostExecute(String result) {
Log.d("mylog", "result= " + result);
delegate.processFinish(result);
}
public String transfer(URL url) throws IOException {
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
// inputStream = url.openStream();
InputStream inputStream = urlConnection.getInputStream();
BufferedReader bin = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
String line = bin.readLine();
StringBuffer sb = new StringBuffer();
while (line != null) {
sb.append(line);
sb.append("\r\n");
line = bin.readLine();
}
inputStream.close();
return sb.toString();
}
}
And the interface is just:
public interface AsyncResponse {
void processFinish(String output);
}
As I said, everytime I switch the phone orientation all the async process is performed (and the Toast shows up). That's exactly what I wanted to avoid in the first place.
you dont need Stop/Resume activities you can use AsyncTask class and doInBackGround method when get data from external url and show to user process Dialog for waiting
Never ever try to get the data from URL on Main thread. Always use AsyncTask for getting the data from URL
You can write in the same activity, but not in the main thread. Maybe AsyncTask will help.

Checking the AsyncTask status seems not working correctly (log doesn't appear on log cat)

I'm trying to see how works an Asynctask class in android. In particular i want reveal in real time the status of the class for see when it is running and when it has finished. For do this, i have created a class that extend the main activity and another class that is the asynctaks class.
This is my main class:
public class PhotoManagement extends Activity{
private String numberOfSelectedPhotos;
private Bitmap currentImage;
private String initConfiguration = "http://www.something.com";
private String response;
private ArrayList<String> formatPhotoList = new ArrayList<String>(); //create a list that will contains the available format of the photos downloaded from the server
private ArrayList<String> pricePhotoList = new ArrayList<String>(); //create a list that will contains the available price for each format of the photos
DownloadWebPageTask webPage = new DownloadWebPageTask();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
protected void onResume(){
super.onResume();
webPage.execute(initConfiguration);
if(webPage.getStatus() == AsyncTask.Status.PENDING){
Log.i("STATUS","PENDING");
}
if(webPage.getStatus() == AsyncTask.Status.RUNNING){
Log.i("","RUNNING");
}
if(webPage.getStatus() == AsyncTask.Status.FINISHED){
Log.i("","FINISHED");
}
}
}
As you can see i want only see the passages of the status with a simple log.
And here there is the asynctask class.
private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... urls) {
for (String url : urls) {
DefaultHttpClient client = new DefaultHttpClient(); //create a new http client
HttpGet httpGet = new HttpGet(url); //create a new http request passing a valid url
try {
HttpResponse execute = client.execute(httpGet); //try to execute the http get request
InputStream content = execute.getEntity().getContent(); //prepare the input stream to read the bytes of the request
BufferedReader buffer = new BufferedReader(
new InputStreamReader(content));
String s = "";
while ((s = buffer.readLine()) != null) {
response += s; //until is present a line to read, the response variable store the value of the lines
}
} catch (Exception e) {
Log.i("MyApp", "Download Exception : " + e.toString()); //Print the error if something goes wrong
}
}
return response; //return the response
}
#Override
protected void onPostExecute(String result) {
result = doInBackground(initConfiguration); //take the result from the DownloadWebPageTask class
result = result.replace("null", "");
Log.i("RESULT",""+result);
//find the price and format value from the result using XmlPullParser
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput( new StringReader ( result ) );
int attributeNumber = xpp.getAttributeCount();
int eventType = xpp.getEventType();
String currentTag = null;
while(eventType != XmlPullParser.END_DOCUMENT){
if(eventType == XmlPullParser.START_TAG) {
currentTag = xpp.getName();
if (currentTag.equals("product")){
xpp.getAttributeValue(null, "name");
formatPhotoList.add(xpp.getAttributeValue(null, "name"));
Log.i("FORMAT PHOTO",""+xpp.getAttributeValue(null, "name"));
}
}
eventType = xpp.next();
}
} catch (XmlPullParserException e) {
e.printStackTrace();
Log.i("","ERROR XML PULL PARSER");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.i("","ERROR IOEXCEPTION");
}
}
}
}
As you can see i have implemented also the method onPostExecute that should be called when the asynctask method has finished to execute the instructions right?
So at this point i don't understand why my log RUNNING and my log FINISHED never appear on the log cat.
What i'm doing wrong?
I'm tried to follow this topic Android, AsyncTask, check status? but in my case it isn't working.
Thanks
Problem :
You are creating object like
DownloadWebPageTask webPage = new DownloadWebPageTask();
But you are calling asynctask on different object,
new DownloadWebPageTask().execute(initConfiguration);
Solution :
It should be like
webPage.execute(initConfiguration);
#Override
protected void onResume(){
super.onResume();
new DownloadWebPageTask().execute(initConfiguration);
here do like this
#Override
protected void onResume(){
super.onResume();
webPage.execute(initConfiguration);
You didn't implement webPage.execute(), add it
Most probably the task hasn't finished or even started yet. As you probably know the AsyncTask is doing it's (background) work on a different thread, so your onResume is running in parallel with it. You can either use the task's get() method to wait for it to finish and get the result of the doInBackground() method and then query for it's status or notify your activity from the task's onPostExecute() method to let it know (and log) that it has finished. I don't recommend you the first option because it will actually block the UI thread and will make your usage of AsyncTask pointless.

General AsyncTask, use one AsyncTask in multiple contexts

I'm using AsyncTask and all the examples I found about an AsyncTask is inside an activity.
I'm trying to make an application with a lot of activity and some must download a HTML page. I don't really want to copy-paste the same code all the time in every activity. I find this to be dirty.
So I need to do it as a special class Async HttpGet and pass the function with an argument. I will execute after the doinbackground (different for every activity).
Is this possible or do I need to copy-paste my code in every activity and change the do in background to do what I need after downloading the HTML page?
Here's an AsyncTask that will download data from a url and update the calling activity.
Make sure your calling activity implements the interface DownloadDataTask.DownloadCompleteHandler and that it passes itself as parameter to the DownloadDataTask constructor.
public class DownloadDataTask extends AsyncTask<String, Integer, String> {
public interface DownloadCompleteHandler
{
public void handleDownloadComplete(String result);
}
private DownloadCompleteHandler handler;
private String url;
public DownloadDataTask(DownloadCompleteHandler handler, String url) {
this.handler = handler;
this.url = url;
}
/* AsyncTask methods */
#Override
protected String[] doInBackground(String... empty) {
return downloadData(url);
}
#Override
protected void onPostExecute(String result) {
handler.handleDownloadComplete(result);
}
/* Downloading Data */
private String downloadData(String urlStr) {
InputStream is = null;
String result = new String();
try {
is = getInputStream(urlStr);
BufferedReader in = new BufferedReader(new InputStreamReader(is));
String inputLine;
while ((inputLine = in.readLine()) != null) {
result += inputLine;
} catch (MalformedURLException ex) {
return "Malformed URL: " + ex.getMessage();
} catch (SocketTimeoutException ex) {
return "Connection timed out";
} catch (IOException ex) {
return "IOException: " + ex.getMessage();
}
finally {
if (is != null)
is.close();
}
return result;
}
private InputStream getInputStream(String urlStr) throws IOException
{
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(7000);
conn.setConnectTimeout(7000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
return conn.getInputStream();
}
}
Just create a class that extends AsyncTask that you can reuse.
public abstract class MyAsyncTask extends AsyncTask<Void, Void, String> {
private final String url;
public MyAsyncTask(String url){
this.url = url;
}
#Override
protected String doInBackground(Void... params){
// get data from url.
return null;
}
}
And then to call it, just create an instance of that class.
new MyAsyncTask("http://www.google.com"){
public void onPostExecute(String result){
// update your views.
}
}.execute();
Well what you can do is create an listener for AsyncTask completion, which listens when your AsyncTask is completed and return you the data. I had created an example to execute database queries in background thread and then returning the data to the Activity. Just check it and you can create similar AsyncTask for your problem.
UPDATE:-
Also you can use BroadCastReceiver as a Listener when your AsyncTask is completed and return the value.
Interface is another option for creating a Listener for AsyncTask.
Here is a demo from my github

Getting a hold of doInBackground(String... params)

In some way I do understand the Handler, but I'm not sure what to do with the params and how to let the code wait until the job is done in the background. I want the UI to be normally working and in the background I want to do an exchange rate calculation.
I have the following:
I call new getOnlineExchangeRate().execute(""); //Get Exchange Rate in BG
After that I want to have a result=amount*exchangerate, but the code is not waiting for the result.
Can somebody tell me how the calculation waits till we have an exchangerate. Do I have to send some params and how would that look?
.
.
.
.
.
public double getYahooExchangeRate(String ER){
double exchangerate=0;
try {
s = getJson("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22"+ER+"%22)&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=");
//s = getJson("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22"+val[from]+val[to]+"%22)&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=");
JSONObject jObj;
jObj = new JSONObject(s);
String exResult = jObj.getJSONObject("query").getJSONObject("results").getJSONObject("rate").getString("Rate");
exchangerate=Double.parseDouble(exResult);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ALS.Toast(myContext.getString(R.string.conversionerror), false);
}
catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ALS.Toast(myContext.getString(R.string.conversionerror), false);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ALS.Toast(myContext.getString(R.string.conversionerror), false);
}
return exchangerate;
}
public String getJson(String url)throws ClientProtocolException, IOException {
StringBuilder build = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String con;
while ((con = reader.readLine()) != null) {
build.append(con);
}
return build.toString();
}
public class getOnlineExchangeRate extends AsyncTask<String, Void, String> {
#Override
protected void onPostExecute(String result) {
// execution of result of Long time consuming operation
ALS.Toast(myContext.getString(R.string.exchangeratesupdated), true);
}
#Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
// perform long running operation operation
getYahooExchangeRate(USDEUR);
return null;
}
I think your problem is in this line:
#Override
protected String doInBackground(String... params) {
getYahooExchangeRate(USDEUR);
return null;
You want to return the result of getYahooExchangeRate and not null :)
So change this and the return-value should be a double. So change this to:
#Override
protected Double doInBackground(String... params){
return getYahooExchangeRate(USDEUR);
}
You also have to change your class header:
public class getOnlineExchangeRate extends AsyncTask<String, Void, Double> {
AsyncTask<Params, Progress, Result>
The generic part tells the AsyncTask which Informationstypes are handled.
The first is the type for the params of doInBackground(Params... )
The second is the type of the progress-Information
The last explains which type is returned by doInBackground(), so it changes the method-header from
protected Result doInBackground(Params... params){ };
to
protected double doInBackground(Params... params){};
To bring back the Result i would use and Observer oder Callback-Pattern.
Edit: changed double to Double, because primitives cannot be used for Generics.
the code is not waiting for the result. Can somebody tell me how the calculation waits till we have an exchangerate. Do I have to send some params and how would that look?
You could use AsyncTask#get() to force the code to wait, but this blocks the main thread until the AsyncTask completes which defies the purpose of using an asynchronous task.
It is best to design your Activity to proceed without the exchange rate, just like my mail app loads allowing me to compose messages and read old messages while the new messages are being fetched. When the asynchronous data loads then you can update your UI with the new information. (I believe this is what you are trying to do.)
To add on to user1885518 code, you should use your AsyncTask as a subclass in your Activity like this:
public class MainActivity extends Activity {
private class getOnlineExchangeRate extends AsyncTask<Void, Void, Double> {
#Override
protected Double doInBackground(Void... params) {
return getYahooExchangeRate(params[0]);
}
#Override
protected void onPostExecute(Double rate) {
// Do something with rate
}
}
...
}
Once you know which exchange rate you want, call:
new getOnlineExchangeRate().execute(USDEUR); //Get Exchange Rate in BG
Now when you have gotten the rate from online, the code calls onPostExecute() with your desired rate. Inside on onPostExceute() you can call whatever method you want in your ACtivity to calculate result=amount*exchangerate and display result wherever it is appropriate.

Categories

Resources