Handling data passed to a result argument of AsyncTask.onPostExecute() - android

I have this private class that is within my main activity, and I am using it pull a JSon object off of my server into my app. The code below works fine and will display the JSon object as a string.
private class HttpAsyncTask extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... urls) {
return httpBuild(urls[0]);
}
// onPostExecute displays the results of the AsyncTask.
#Override
protected void onPostExecute(String result) {
Toast.makeText(getBaseContext(), "Received!", Toast.LENGTH_LONG).show();
etResponse.setText(result);
}
}
what I am trying to do is place change the onPostExecute() method so it acts like webResult = result where webResult is an instance variable of the class mainActivity The problem is once I do this when I try to put the below code into the onCreate() method after HTTpAsyncTask has been called the app fails to display the object and crashes.
public class MainActivity extends Activity {
private static final String mainSite = "http://mysitehere";
private String webResult;
// private JSONArray floorsInBuilding, roomsInGender;
// private JSONObject room;
// private JSONArray arrayOfFloors;
// private JSONObject room, arrayOfRooms;
EditText etResponse;
TextView tvIsConnected;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get reference to the views
etResponse = (EditText) findViewById(R.id.etResponse);
tvIsConnected = (TextView) findViewById(R.id.tvIsConnected);
// check if you are connected or not
if(isConnected()){
tvIsConnected.setBackgroundColor(0xFF00CC00);
tvIsConnected.setText("You are conncted");
}
else{
tvIsConnected.setText("You are NOT conncted");
}
// call AsynTask to perform network operation on separate thread
new HttpAsyncTask().execute(this.buildBuildingAddress(8));
Toast.makeText(getBaseContext(), "Received!", Toast.LENGTH_LONG).show();
etResponse.setText(WebResult);
}
I'm wondering what makes the part of the code that displays the result dependent on the HttpAsyncTask. I'm also wondering how I can get the result of the HttpAsyncTask and store it as a string in the main class.
A good chunk of my code is based of of this example.
http://hmkcode.com/android-parsing-json-data/
I'm sorry If my knowledge of android isn't so great but my experience lies in more in java.

If you want to wait till the task is over so that you can override the result with webResult then you may use the asyncTask method onProgressUpdate which runs on UI thread. You may refer android developer site to know how to use that method.

The answer to this is that the asynchronous task runs alongside onCreate(). So you have to create some piece of code that waits for the task to complete or you have to manipulate the result of the async task in the onPostExecute() method

Related

Set text after complete thread

I have a problem. Why a data in setText method are set incorrecetly?
MainActivity class
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewCity = (TextView) findViewById(R.id.text_view_city_name);
textViewTemperature = (TextView) findViewById(R.id.text_view_current_temperature);
new Thread(new WeatherYahoo()).start();
Weather weather = new Weather();
textViewCity.setText(weather.getCity());
textViewTemperature.setText(String.valueOf(weather.getTemperature()));
}
Data were downloaded and set correctly in Weather class (I use JSON) but on the screen showed empty string form textViewCity and 0 for textViewTemperature.
Everything in your activity executes on the UI thread. So this is happening because you are trying to set the text right after you started a new Thread with WeatherYahoo, so you don't wait for the result, but just output empty value. I would recommend you to use AsyncTask for such kind of calls and retrieving results on UI thread. So you can do all your work you do in WeatherYahoo class in doInBackground() method instead and output the result in onPostExecute() method. As an example:
private class WeatherYahooTask extends AsyncTask<Void, Void, Weather> {
protected Weather doInBackground(Void... params) {
// do any kind of work you need (but NOT on the UI thread)
// ...
return weather;
}
protected void onPostExecute(Weather weather) {
// do any kind of work you need to do on UI thread
textViewCity.setText(weather.getCity());
textViewTemperature.setText(String.valueOf(weather.getTemperature()));
}
}
You have 2 options:
Wait for the thread to finish downloading the json using:
Thread t = new Thread(new WeatherYahoo()).start();
t.join();
Weather weather = new Weather();
Or you can use asynctasks like Yuriy posted.

Android - do something in MainActivity after AsyncTask is done

I've got an AsyncTask class that will do a HttpGet-request. I want to do something after this AsyncTask is done, but from within my MainActivity.
Here is my TaskGetAPI class:
public class TaskGetAPI extends AsyncTask<String, Void, String>
{
private TextView output;
private Controller controller;
public TaskGetAPI(TextView output){
this.output = output;
}
#Override
protected String doInBackground(String... urls){
String response = "";
for(String url : urls){
HttpGet get = new HttpGet(url);
try{
// Send the GET-request
HttpResponse execute = MainActivity.HttpClient.execute(get);
// Get the response of the GET-request
InputStream content = execute.getEntity().getContent();
BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
String s = "";
while((s = buffer.readLine()) != null)
response += s;
content.close();
buffer.close();
}
catch(Exception ex){
ex.printStackTrace();
}
}
return response;
}
#Override
protected void onPostExecute(String result){
if(!Config.LOCALHOST)
output.setText(result);
else
controller = Controller.fromJson(result);
}
public Controller getController(){
return controller;
}
And here is the method from my MainActivity where I use this class:
private void sendGetRequest(){
...
// Web API GET-request
if(!get_url.equals("") && get_url != null){
TaskGetAPI task = new TaskGetAPI(output);
task.execute(new String[] { get_url });
// TODO: When AsyncTask is done, do:
controller = task.getController();
Log.i("CONTROLLER", controller.toString());
}
}
As you can see I set the Controller that I use later on in the onPostExecute-method of the AsyncTask.
Since this counters the entire purpose of Async tasks I first thought of removing the extends AsyncTask and just make a regular class & method of my HttpGet, but then I get a android.os.NetworkOnMainThreadException, meaning I need to use an AsyncTask (or something similar) to use HttpGet from within a different thread than my MainThread.
So, does anyone know what I should put at the // TODO?
I did try adding a boolean field (isDone) to the TaskGetAPI class with a getter and then use:
while(true){
if(task.isDone()){
Controller controller = task.getController();
Log.i("CONTROLLER", controller.toString());
}
}
But then the following steps occur:
doInBackground of the TaskGetAPI class is completely done.
Now we are stuck in this while(true)-loop..
and onPostExecute which sets the isDone to true is never called.
When the task will get finish then it will call onPostExecute, So you can use LocalBroadcast to send the broadcast message to main activity. You can use sendBroadcast which uses asynchronous manner i.e. send broadcast to all listener at a time rather than sendOrderedBroadcast.
Assuming that you only use TaskGetAPI inside your Activity, you can just define TaskGetAPI as an inner class, like the example in https://developer.android.com/guide/components/processes-and-threads.html#AsyncTask
public class MainActivity extends Activity {
public class TaskGetAPI extends AsyncTask<String, Void, String> {
/*
* This object is instanced inside the MainActivity instance,
* so it has access to MainActivity methods and members
* (including private ones).
* Remember to only access to UI elements like views from the main thread,
* that is, only from onPreExecute, onProgress or onPostExecute
* In this class "this" makes reference to the TaskGetAPI instance,
* if you need a refeence to the MainActivity instance, use MainActivity.this
*
*/
protected String doInBackground(String... urls){ ... }
protected void onPostExecute(String result){
mMyTextView.setText(result);
}
}
private TextView mMyTextView;
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main);
mMyTextView = (TextView) findViewById(R.id.view);
new TaskGetAPI().exec();
}
}
If you need to use TaskGetAPI you can define it outside MainActivity and define a subclass as inner class of MainActivity.
There are other options, though, like defining listeners (like the onClickListeners) and call them in onPostExecute, but that is unnecesarily complex.
You can send a broadcast with your result from onPostExecute with the response. The activity will listen to it and execute the code you want.
while(true) is never a good idea, especially on mobiles where battery life is important.

In Android, How do I change the TextView from an AsyncTask? [duplicate]

This question already has answers here:
AsyncTask Android example
(21 answers)
Closed 9 years ago.
I have a splash screen with a TextView to displays what the app is doing such as "Updating Library"... "Updating Shipping"... etc. I'm using AsyncTask to updated my database via an API.
I'm passing the update text to the AsyncTask. I need to change the text in TextView statusMessage. I'm attempting to do this:
public class JSONParser extends AsyncTask<String, String, JSONObject> {
static InputStream is = null;
static JSONObject json = null;
static String outPut = "";
TextView statusMessage;
#Override
protected void onPreExecute() {
statusMessage = (TextView) findViewById(R.id.statusMessage);
}
...
My plan is to change the text in the doInBackground method but findViewById isn't accessible in AsyncTask. I think I need to use setContentView to allow findViewById to work but I'm not sure how.
My java file is SplashScreen.java and my xml is activity_splash_screen.xml
----- EDIT -----
For more info I have three pieces talking to each other:
SplashScreen.java -> calls to method in baseActivity.java -> method sends data to JSONParser.java -> sends parsed JSON from the API to baseActivity.java to update database
Per suggestions below I've declared
statusMessage = (TextView) findViewById(R.id.statusMessage);
In baseActivity.java's onCreate since it's the file calling the AsyncTask.
In JSONParser.java I've done this, now:
public class JSONParser extends AsyncTask<String, String, JSONObject> {
static InputStream is = null;
static JSONObject json = null;
static String outPut = "";
TextView statusMessage;
#Override
protected JSONObject doInBackground(String... params) {
// TODO Auto-generated method stub
...
}
protected void onProgressUpdate() {
statusMessage.setText("testing");
}
protected void onPostExecute(JSONObject result) {
}
}
I'm just using "testing" there for testing purposes.
My plan is to change the text in the doInBackground
Bad plan! You can't update the UI from a background Thread. You will need to do this in either onPostExecute() or onProgressUpdate().
but findViewById isn't accessible in AsyncTask.
If this is an inner class of your Activity then initialize the View in the Activity then update it in your task as described above.
If it is its own file then you will want to use an interface and have a callback to the Acitivty in onPostExecute(), onPreExecute(), or onProgressUpdate(). You can see an example of that in this SO answer.
I think I need to use setContentView to allow findViewById
Definitely! But as stated above, do this before the task such as in onCreate() of your Activity.
Edit
onProgressUpdate() takes a param but your onProgressUpdate() doesn't so it isn't the same method. That's why it complained when you had #Override which is the point of the annotation. It complains and you know you are suppose to be overriding a method so you know something is wrong with it.
Change it to
protected void onProgressUpdate(Void...values) {
statusMessage.setText("testing");
}
onProgressUpdate() link
http://developer.android.com/reference/android/os/AsyncTask.html#onProgressUpdate(Progress...)
You should use onProgressUpdate, that method has acces to the ui thread.
public class yourAsync extends AsyncTask<> {
#Override
protected void onProgressUpdate() {
textView.setText();
}
}
put something like this in your activity
Handler statusUpdateHandeler = new Handler()........
In your thread, call the handler (send it a message)
MainActivity.statusUpdateHandler.sendEmptyMessage(1);
In you actual handler code, set the status message.
I think I need to use setContentView to allow findViewById to work but I'm not sure how.
Yes. You need to use setContentView(R.layout.activity_splash_screen) in onCreate.
You can initialize your view in onCreate make asynctask an inner class of activity and update ui in onPreExecute.
TextView statusMessage;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
statusMessage = (TextView) findViewById(R.id.statusMessage);
}
Also you can use a progressdialog and display the message. I think using progressdialog would be a better choice than textview. You can publish progress in doInbackground and update progress dialog in onProgressUpdate()
http://developer.android.com/reference/android/os/AsyncTask.html

Android: Trying to use AsyncTask to talk to web server

I'm trying to use doInBackground method of AsyncTask to send a message to a webserver. Then use the onPreExecute() and onPostExecute(String result) methods of AsyncTask to change a text control from sending data to Fineshed.
The issue is that Inside the AsyncTask String class I cannot access any of the variables declared in the outside class. Thus I cannot change my TextView inside these methods. I get, so mSEnd.setText("Sending data")gives me mSend undefined.
Is there a way to use the variables I declare in my outside class?
public class EndOfWorldActivity extends cBase implements OnClickListener {
TextView textCountDown;
TextView textPercent;
public void onClick(View v) {
Intent i;
switch(v.getId())
{
case R.id.butVote3:
// Start ASync Task
new SendTextOperation().execute("");
break;
case R.id.buGame:
// Start ASync Task
new SendTextOperation().execute("");
break;
}
}
private class SendTextOperation extends AsyncTask<String, Void, String> {
#Override
protected void onPreExecute() {
//Update UI here
mSEnd undefined error
mSend.setText("Sending your vote to server");
mSend.invalidate();
}
#Override
protected String doInBackground(String... params) {
// Talk to server here to avoid Ui hanging
// talk to server method undefined
TalkToServer( mYes[mPes-1] );
return null;
}
#Override
protected void onPostExecute(String result) {
// Update screen here after talk to server end
UpdateScreen();
mSend .setText("");
}
}
} // end of class
use
new SendTextOperation().execute();
instead of this
new SendTextOperation().execute("");
There is a complete example given in the link below.
The application send HTTP query to a web server and get back the content of the query :
Example: Android bi-directional network socket using AsyncTask

Android: How can I pass parameters to AsyncTask's onPreExecute()?

I use an AsyncTask for loading operations that I implemented as an inner class.
In onPreExecute() I show a loading dialog which I then hide again in onPostExecute(). But for some of the loading operations I know in advance that they will finish very quickly so I don't want to display the loading dialog.
I wanted to indicate this by a boolean parameter that I could pass to onPreExecute() but apparently for some reason onPreExecute() doesn't take any parameters.
The obvious workaround would probably be to create a member field in my AsyncTask or in the outer class which I would have to set before every loading operation but that does not seem very elegant. Is there a better way to do this?
You can override the constructor. Something like:
private class MyAsyncTask extends AsyncTask<Void, Void, Void> {
public MyAsyncTask(boolean showLoading) {
super();
// do stuff
}
// doInBackground() et al.
}
Then, when calling the task, do something like:
new MyAsyncTask(true).execute(maybe_other_params);
Edit: this is more useful than creating member variables because it simplifies the task invocation. Compare the code above with:
MyAsyncTask task = new MyAsyncTask();
task.showLoading = false;
task.execute();
1) For me that's the most simple way passing parameters to async task
is like this
// To call the async task do it like this
Boolean[] myTaskParams = { true, true, true };
myAsyncTask = new myAsyncTask ().execute(myTaskParams);
Declare and use the async task like here
private class myAsyncTask extends AsyncTask<Boolean, Void, Void> {
#Override
protected Void doInBackground(Boolean...pParams)
{
Boolean param1, param2, param3;
//
param1=pParams[0];
param2=pParams[1];
param3=pParams[2];
....
}
2) Passing methods to async-task
In order to avoid coding the async-Task infrastructure (thread, messagenhandler, ...) multiple times you might consider to pass the methods which should be executed in your async-task as a parameter. Following example outlines this approach.
In addition you might have the need to subclass the async-task to pass initialization parameters in the constructor.
/* Generic Async Task */
interface MyGenericMethod {
int execute(String param);
}
protected class testtask extends AsyncTask<MyGenericMethod, Void, Void>
{
public String mParam; // member variable to parameterize the function
#Override
protected Void doInBackground(MyGenericMethod... params) {
// do something here
params[0].execute("Myparameter");
return null;
}
}
// to start the asynctask do something like that
public void startAsyncTask()
{
//
AsyncTask<MyGenericMethod, Void, Void> mytest = new testtask().execute(new MyGenericMethod() {
public int execute(String param) {
//body
return 1;
}
});
}
why, how and which parameters are passed to Asynctask<>, see detail here. I think it is the best explanation.
Google's Android Documentation Says that :
An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called onPreExecute, doInBackground, onProgressUpdate and onPostExecute.
AsyncTask's generic types :
The three types used by an asynchronous task are the following:
Params, the type of the parameters sent to the task upon execution.
Progress, the type of the progress units published during the background computation.
Result, the type of the result of the background computation.
Not all types are always used by an asynchronous task. To mark a type as unused, simply use the type Void:
private class MyTask extends AsyncTask<Void, Void, Void> { ... }
You Can further refer : http://developer.android.com/reference/android/os/AsyncTask.html
Or You Can clear whats the role of AsyncTask by refering Sankar-Ganesh's Blog
Well The structure of a typical AsyncTask class goes like :
private class MyTask extends AsyncTask<X, Y, Z>
protected void onPreExecute(){
}
This method is executed before starting the new Thread. There is no input/output values, so just initialize variables or whatever you think you need to do.
protected Z doInBackground(X...x){
}
The most important method in the AsyncTask class. You have to place here all the stuff you want to do in the background, in a different thread from the main one. Here we have as an input value an array of objects from the type “X” (Do you see in the header? We have “...extends AsyncTask” These are the TYPES of the input parameters) and returns an object from the type “Z”.
protected void onProgressUpdate(Y y){
}
This method is called using the method publishProgress(y) and it is usually used when you want to show any progress or information in the main screen, like a progress bar showing the progress of the operation you are doing in the background.
protected void onPostExecute(Z z){
}
This method is called after the operation in the background is done. As an input parameter you will receive the output parameter of the doInBackground method.
What about the X, Y and Z types?
As you can deduce from the above structure:
X – The type of the input variables value you want to set to the background process. This can be an array of objects.
Y – The type of the objects you are going to enter in the onProgressUpdate method.
Z – The type of the result from the operations you have done in the background process.
How do we call this task from an outside class? Just with the following two lines:
MyTask myTask = new MyTask();
myTask.execute(x);
Where x is the input parameter of the type X.
Once we have our task running, we can find out its status from “outside”. Using the “getStatus()” method.
myTask.getStatus();
and we can receive the following status:
RUNNING - Indicates that the task is running.
PENDING - Indicates that the task has not been executed yet.
FINISHED - Indicates that onPostExecute(Z) has finished.
Hints about using AsyncTask
Do not call the methods onPreExecute, doInBackground and onPostExecute manually. This is automatically done by the system.
You cannot call an AsyncTask inside another AsyncTask or Thread. The call of the method execute must be done in the UI Thread.
The method onPostExecute is executed in the UI Thread (here you can call another AsyncTask!).
The input parameters of the task can be an Object array, this way you can put whatever objects and types you want.
You can either pass the parameter in the task constructor or when you call execute:
AsyncTask<Object, Void, MyTaskResult>
The first parameter (Object) is passed in doInBackground.
The third parameter (MyTaskResult) is returned by doInBackground. You can change them to the types you want. The three dots mean that zero or more objects (or an array of them) may be passed as the argument(s).
public class MyActivity extends AppCompatActivity {
TextView textView1;
TextView textView2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
textView1 = (TextView) findViewById(R.id.textView1);
textView2 = (TextView) findViewById(R.id.textView2);
String input1 = "test";
boolean input2 = true;
int input3 = 100;
long input4 = 100000000;
new MyTask(input3, input4).execute(input1, input2);
}
private class MyTaskResult {
String text1;
String text2;
}
private class MyTask extends AsyncTask<Object, Void, MyTaskResult> {
private String val1;
private boolean val2;
private int val3;
private long val4;
public MyTask(int in3, long in4) {
this.val3 = in3;
this.val4 = in4;
// Do something ...
}
protected void onPreExecute() {
// Do something ...
}
#Override
protected MyTaskResult doInBackground(Object... params) {
MyTaskResult res = new MyTaskResult();
val1 = (String) params[0];
val2 = (boolean) params[1];
//Do some lengthy operation
res.text1 = RunProc1(val1);
res.text2 = RunProc2(val2);
return res;
}
#Override
protected void onPostExecute(MyTaskResult res) {
textView1.setText(res.text1);
textView2.setText(res.text2);
}
}
}

Categories

Resources