Is it possible to run the "AsyncTask" method inside another method "AsyncTask"? - java

I have an activity that when started makes a call to a "json" for get data categories of songs, after that I make a call to the method "AsyncTask" for the list of songs that category from another "JSON "the problem is that when I start the activity, this is locked , after 2 seconds, the activity opens the layout and I can see the categories on the action bar and not because the songs are looking for in the background.
main activity (onCreate):
java.io.InputStream source = null;
source = retrieveStream(UrlApi.URL_BASE + UrlApi.URL_STORE + _bundle.getString("_id") + UrlApi.CATEGORY_SONG);
Log.i("URL - KARAOKE", UrlApi.URL_BASE + UrlApi.URL_STORE + _bundle.getString("_id") + UrlApi.CATEGORY_SONG);
Reader reader = new InputStreamReader(source);
Type happyCollection = new TypeToken<Collection<String>>() {}.getType();
_karaoke_category_response = new Gson().fromJson(reader, happyCollection);
if(_karaoke_category_response.size() < 1){
finish();
Toast.makeText(getApplicationContext(), "Local sin karaokes", Toast.LENGTH_SHORT).show();
}else{
Log.i("Category - response", _karaoke_category_response.toString());
_karaoke_category_adapter = new ArrayAdapter<String>(getSupportActionBar().getThemedContext(), R.layout.spinner_item,_karaoke_category_response);
getSupportActionBar().setListNavigationCallbacks(_karaoke_category_adapter, this);
}
The follow code is of search the songs of that categori and set it
class AsyncKaraoke extends AsyncTask<Void, Void, Void> {
String category;
public AsyncKaraoke(String category) {
this.category = category;
}
protected void onPreExecute(){
super.onPreExecute();
setSupportProgressBarIndeterminateVisibility(true);
}
#Override
protected Void doInBackground(Void... params) {
java.io.InputStream source = null;
try {
source = retrieveStream(UrlApi.URL_BASE + UrlApi.URL_STORE + _bundle.getString("_id") + UrlApi.KARAOKE_URL + UrlApi.FILTER_CATEGORY + URLEncoder.encode(category, "UTF-8"));
Log.i("URL - KARAOKE", UrlApi.URL_BASE + UrlApi.URL_STORE + _bundle.getString("_id") + UrlApi.KARAOKE_URL + UrlApi.FILTER_CATEGORY + URLEncoder.encode(category, "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Reader reader = new InputStreamReader(source);
Type karaokeCollection = new TypeToken<Collection<KaraokeModel>>() {}.getType();
_response = new Gson().fromJson(reader, karaokeCollection);
Log.i("Response - KaraokeCategory" , _karaoke_category_response.toString());
return null;
}
protected void onPostExecute(Void Void){
super.onPostExecute(Void);
setSupportProgressBarIndeterminateVisibility(false);
_karaoke_adapter = new KaraokeAdapter(KaraokeActivity.this, _bundle.getString("_id"), _response);
if(_response.size() == 0){
Toast.makeText(getApplicationContext(), "Categoria sin karaoke", Toast.LENGTH_SHORT).show();
}
_list_view.setAdapter(_karaoke_adapter);
_karaoke_adapter.notifyDataSetChanged();
}
}
How should I do to call 2 times to "AsyncTask" method and prevent the activity is engaged by a few seconds?

The primary rule of AsyncTask is that it must always be create and run on the main thread. You will get an exception if you start another AsyncTask inside the doInBackground() method. Your options are to start the next AsyncTask in one of the callbacks. Generally, some people will chain AsyncTask in the onPostExecute() method, but you can also start them in onPreExecute() and onProgressUpdate().
EDIT:
Additionally, you can run AsyncTask in sequence of each other using AsyncTask#executeOnExecutor(). From HoneyComb on, you don't need to do this. All AsyncTask run in a serial thread pool in the order they are executed. Though it may be easier to understand that the code is running serially if you use it. You do need to chain if using Android Android 1.6 - 2.3.x though.

You should build the URL in the main activity, then run an AsyncTask to download the content and finally process the result back in your activity.
The syntax to run an AsyncTask is:
String category = "...";
new AsyncKaraoke().execute(category);
You can also remove the onPostExecute() method from your AsyncKaraoke class and put it in the activity:
String category = "...";
new AsyncKaraoke() {
#Override
protected void onPostExecute(Void Void){
// do stuff (and moving the third type of the AsyncKaraoke to something else
// than Void will allow you to get the result here.
}.execute(category);

Generally, we use AsyncTask to perform an action in another thread than the UI thread to prevent the user from being halt while performing some actions. SO, it does not make any sense to create an additional AsyncTask inside the outer one. Try to manage your code to do it all the those method soInBackground(), onPreExecution() and onPostExecution() and make use of their order of execution

int count = 0;
protected void onPostExecute(Void Void){
super.onPostExecute(Void);
// call same asynctask
if (count == 0)
{
execute asynctask
count++;
}
}

Related

How does AsyncTask work one process to another one?

I'm currently studying android on my own and pretty new to java. I'm wondering how AsyncTask works like this: onPreExecute() -> doInBackground() -> onPostExecute(). When I look at others define their AsynTask, it seems like only method is declared in their code with no calls upon the method. I can't figure out how doInBackground() comes after onPreExecute() with no code that links both like:
onPreExecute(){ ~~~~~ call doInBackground()}
My point is that when AsyncTask.execute() is called, onPreExecute() is called, then doInBackground(), finally onPostExecute(). I couldn't find any code in library that actually connects these together. All I could find is this:
#MainThread
public final AsyncTask<Params, Progress, Result> execute(Params... params) {
return executeOnExecutor(sDefaultExecutor, params);
#MainThread
public final AsyncTask<Params, Progress, Result> executeOnExecutor(Executor exec,
Params... params) {
if (mStatus != Status.PENDING) {
switch (mStatus) {
case RUNNING:
throw new IllegalStateException("Cannot execute task:"
+ " the task is already running.");
case FINISHED:
throw new IllegalStateException("Cannot execute task:"
+ " the task has already been executed "
+ "(a task can be executed only once)");
}
}
mStatus = Status.RUNNING;
onPreExecute();
mWorker.mParams = params;
exec.execute(mFuture);
return this;
}
Here when AsyncTask.execute() is called, onPreExecute() is called. But without any connection to doInBackground the task works just fine. I feel like I'm missing some fundamental logic or process of java or android. Plz, help me with this unsolved question in mind. Sample code is shown below. Thank you in advance.
#Override
protected void onPreExecute() {
super.onPreExecute();
mLoadingIndicator.setVisibility(View.VISIBLE);
}
#Override
protected String[] doInBackground(String... params) {
/* If there's no zip code, there's nothing to look up. */
if (params.length == 0) {
return null;
}
String location = params[0];
URL weatherRequestUrl = NetworkUtils.buildUrl(location);
try {
String jsonWeatherResponse = NetworkUtils
.getResponseFromHttpUrl(weatherRequestUrl);
String[] simpleJsonWeatherData = OpenWeatherJsonUtils
.getSimpleWeatherStringsFromJson(MainActivity.this, jsonWeatherResponse);
return simpleJsonWeatherData;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
#Override
protected void onPostExecute(String[] weatherData) {
// COMPLETED (19) As soon as the data is finished loading, hide the loading indicator
mLoadingIndicator.setVisibility(View.INVISIBLE);
if (weatherData != null) {
// COMPLETED (11) If the weather data was not null, make sure the data view is visible
showWeatherDataView();
/*
* Iterate through the array and append the Strings to the TextView. The reason why we add
* the "\n\n\n" after the String is to give visual separation between each String in the
* TextView. Later, we'll learn about a better way to display lists of data.
*/
for (String weatherString : weatherData) {
mWeatherTextView.append((weatherString) + "\n\n\n");
}
} else {
// COMPLETED (10) If the weather data was null, show the error message
showErrorMessage();
}
I guess you shouldn't waste time on AsyncTask since it is deprecated.
Instead you should focus on coroutines, recommended by google here , or some other state of the art framework to achive what you want (e.g. rx java)
Yes, you are correct. The logic is onPreExecute() -> doInBackground() -> onPostExecute()
Synchronous VS asynchronous
You can read this article for a better understanding even though it's using Javascript to explain it.

How to wait until method from another class in asynctask finishes in android

I am newbie in AsyncTask, and I got problem.
I have AsyncTask, which saves info in DB by calling function from another class. The problem is that onPostExecute method is calling before my function finishes; Here is my code:
class checkNewReviews2 extends AsyncTask<List<ReviewList.ReviewItem>, Void, Void>{
#Override
protected Void doInBackground(List<ReviewList.ReviewItem>... reviewList) {
int size = reviewList[0].size();
if(size>0) {
for (int i = 0; i < size; i++) {
ReviewList.ReviewItem r = reviewList[0].get(i);
ContentValues c = new ContentValues();
c.put(LentaClass.LentaListEntry.FILM_ID, r.getFilm_id());
c.put(LentaClass.LentaListEntry.USER_ID, r.getUser_id());
c.put(LentaClass.LentaListEntry.REVIEW_TEXT, r.getReview_text());
c.put(LentaClass.LentaListEntry.CREATED_AT, r.getCreated_at());
c.put(LentaClass.LentaListEntry.REVIEW_TYPE, r.getReview_type());
c.put(LentaClass.LentaListEntry.VIEWS, r.getViews());
sqLiteDatabase.insert(LentaClass.LentaListEntry.TABLE_NAME, null, c);
c.clear();
}
FilmHandler filmHandler = new FilmHandler(getContext());
filmHandler.HandleFilms(reviewList[0]);
UserHandler userHandler = new UserHandler(getContext());
userHandler.HandleUsers(reviewList[0]);
}
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
initiateRecyclerView();
}
}
I already tried to put all calls into onPreExecute, but the result is still the same. Also as a notice, the first block of sql code (cycle) is succesfully handled, not as Film and User handlers. How should I call initiateRecyclerView after AsyncTask completely executed?
Your doInBackgound() method needs to "wait" for your FilmHandler, UserHandler classes to finish.
My guess is that your classes are working on a background thread so when you use them your code immediately continues to the return null statement - and finishing the background work, causing onPostExecute() to be called.
Change your classes implementation to work on the calling thread and not create their own.
Can you please add context to doinbackground parameter as well.Might be getcontext returning null/Application Context
onPostExecute will execute only after completion of doInBackground. If the FilmHandler and Userhandler are implemented on separate thread , it can cause the issue. So to avoid this,do not use another thread to save data in DB (or) Performing some other sort of operatio instead complete the entire saving data in the doInBackground itself.
Else there is an another way which also you can archive the functionality. You need to add a call back for the filmHandler or userHandler functions and check for the update in doinBackground and based on it you can pass the result to PostExcecute()
class checkNewReviews2 extends AsyncTask<List<ReviewList.ReviewItem>, Void, Boolean>{
#Override
protected Void doInBackground(List<ReviewList.ReviewItem>... reviewList) {
int size = reviewList[0].size();
if(size>0) {
for (int i = 0; i < size; i++) {
ReviewList.ReviewItem r = reviewList[0].get(i);
ContentValues c = new ContentValues();
c.put(LentaClass.LentaListEntry.FILM_ID, r.getFilm_id());
c.put(LentaClass.LentaListEntry.USER_ID, r.getUser_id());
c.put(LentaClass.LentaListEntry.REVIEW_TEXT, r.getReview_text());
c.put(LentaClass.LentaListEntry.CREATED_AT, r.getCreated_at());
c.put(LentaClass.LentaListEntry.REVIEW_TYPE, r.getReview_type());
c.put(LentaClass.LentaListEntry.VIEWS, r.getViews());
sqLiteDatabase.insert(LentaClass.LentaListEntry.TABLE_NAME, null, c);
c.clear();
}
FilmHandler filmHandler = new FilmHandler(YourActivity.this);
boolean filmHandlerState = filmHandler.HandleFilms(reviewList[0]);
UserHandler userHandler = new UserHandler(YourActivity.this);
boolean userHandlerState = userHandler.HandleUsers(reviewList[0]);
if(filmHandlerState && userHandlerState )
{
return true;
}
else
{
return false;
}
}
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if(result)
{
initiateRecyclerView();
}
else
{
// any one of the filmHandler or userHandler function has failed. so do your handling here
}
}
In another point as stated by #user3413619 might be getContext() is returning null so, try to make it YourActivity.this
make doInBackground method return type boolean and pass result to postExecute boolean then check if result true do what method you want to call.
Thanks

Running FileOutputStream operations in AsyncTask's doInBackground() method does not execute successfully

I am using an AsyncTask as an inner class to save a bitmap. But the FileOutputStream operation does not work. If I run the same code in a method in the UI thread the code runs and I am able to successfully save the bitmap.
I know that the doInBackground() method does execute and the onPostExecute() method gets executed as well.
Also, if I take the input stream which is passed to the AsyncTask, and try and decode it to set an imageView, it does not work. But if I use the same code outside of the AsyncTask it works.
I implemented the AsyncTask as follows and the 2nd and 3rd log statements do not get logged, so I know something is not running properly:
public class SaveImageToInternalStorage extends AsyncTask {
InputStream bitmap;
String PICTURE_KEY;
public SaveImageToInternalStorage1(final InputStream bitmap, final String PICTURE_KEY) {
this.bitmap = bitmap;
this.PICTURE_KEY = PICTURE_KEY;
}
#Override
protected Object doInBackground(Object[] params) {
FileOutputStream fos;
try {
fos = picture_chooser.this.openFileOutput(PICTURE_KEY, MODE_PRIVATE);
Bitmap bitmap2 = BitmapFactory.decodeStream(bitmap);
Log.v("saveBitmap", " first log statement"); ////This gets logged
bitmap2.compress(Bitmap.CompressFormat.WEBP, 85, fos);
Log.v("saveBitmap", " second log statement"); // This is not logged
fos.close();
Log.v("saveBitmap", " third log statement"); // This is not logged
} catch (Exception e) {
Log.e("saveToInternalStorage()", e.getMessage());
}
return null;
}
#Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
Log.v("saveBitmap", " onPostExecute log statement"); // This is logged
imageViewSetter(bitmap);
}
}
//runs in the wrapper class
public void imageViewSetter(InputStream inputStream) {
imageView.setImageBitmap(BitmapFactory.decodeStream(inputStream)); //this does not set the imageView
}
Any help would be greatly appreciated.
Have a look at this example from the Android reference here
I've recreated the code for you below
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
// Escape early if cancel() is called
if (isCancelled()) break;
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}
Notice the class declaration at the top is followed by
<URL, Integer, Long>
Next, look at each of the three overriden methods inside the class. You will notice that URL refers to the argument passed in to doInBackground, Integer is what is output by onProgressUpdate and Long is what is returned by your doInBackground method to onPostExecute
From this you can hopefully see that AsyncTask should be used as follows
doInBackground - pass in whatever it is you want to do some work on. This method does the work you want on a background thread. This method needs to return the finished object(s) toonPostExecute which is where you can catch the results of the work done and update the UI if you want to.
onProgressUpdate is optional, you don't need to do anything here.

Android - AsyncTask or Thread while recovering data to SQLite

I'm having trouble figuring out how to make this work.
I'm developing an app in which I download data from a online txt, parse it and add it to my database. This is taking +-30 seconds, and there is only one part of the updater implemented.
When I try to use a Thread or Asynctask, I have problems when trying to pass the arguments to the void which is updating the Database.
How can I implement it on the good way?
Here is my code:
Main activity class:
public class Androideye_principal extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
//WhazzupUpdate.DownloadWhazzup(AllUsers, TotalConnections);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_androideye_principal);
PilotsSQLiteHelper pdbh = new PilotsSQLiteHelper(this, "PilotsDB", null, 1);
SQLiteDatabase dbPilots = pdbh.getWritableDatabase();
Context context = getApplicationContext();
String TotalConnections = new String();
WhazzupUpdate.DownloadWhazzup(dbPilots, TotalConnections, context);
dbPilots.close();
CharSequence text = "Total Users: " + TotalConnections;
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
[Continues with Buttons stuff and so on...]
Parser class:
public class WhazzupUpdate {
public static void DownloadWhazzup (SQLiteDatabase PilotsDB, String TotalConnections, Context context) {
try {
// Create a URL for the desired page
URL url = new URL("This is my url/whazzup.txt");
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
for (int i = 0; i<=4; i++) {
in.readLine(); } // Go to the number of connections
TotalConnections = in.readLine();
for (int i = 0; i<=3; i++) {
in.readLine(); } // Go to !CLIENTS
PilotsDB.execSQL("DELETE FROM Pilots");
while (((str = in.readLine()) != null) && !(in.readLine().contains("!AIRPORTS"))) {
// str is one line of text; readLine() strips the newline character(s)
String[] dataRow = str.split(":");
if (str.contains("PILOT")) {
ContentValues NewValue = new ContentValues();
NewValue.put("VID", dataRow[1]);
[More NewValue puts...]
PilotsDB.insert("Pilots", null, NewValue);
} //End IF Pilot
} // End While
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
}}
As you see, I call WhazzupUpdate.DownloadWhazzup Method in the main activity, and this is when all is getting frozen, but don't know how to derivate it to another threat and keep the references to the Data Bases and so on...
Hope anyone can help me. Thanks in advance.
A Thread or AsyncTask would be fine here. I prefer using AsyncTask for most of my heavy-lifting. You can create an AsyncTask and do your work in doInBackground() as it works on a background Thread. Then you can update your UI elements if needed in any of its other methods.
onPostExecute() will run after a result is passed from doInBackground()
onProgressUpdate() will run if you need to update UI during doInBackground() operations by calling publishProgress(). Here you can show a ProgressBar if you need to
and
onPreExecute() will run when you first call your task before doInBackground() runs.
Running the code in a background thread using Thread or AsyncTask will allow your UI to be free while the heavy work is being done.
Example of AsyncTask
Using interface with AsyncTask to post data back yo UI
AsyncTask Docs

Unable to call methods from onPostExecute

I am using the an asynchronous task to run a JSON downloader as thus: (abridged)
public class JSONDownloader extends AsyncTask<Object, Object, Object>{
#Override
protected Object doInBackground(Object... params) {
if(JSONstate == false){
try {
final URL url = new URL([REDACTED]);
final URLConnection urlConnection = url.openConnection();
urlConnection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
urlConnection.connect();
final InputStream inputStream = urlConnection.getInputStream();
final StringBuilder sb = new StringBuilder();
while (inputStream.available() > 0) {
sb.append((char) inputStream.read());
}
String result = sb.toString();
JSONObject jsonOrg = new JSONObject(result);
String ok = "ok";
Response = jsonOrg.getString("response");
System.out.println(Response);
if(Response.equals(ok)){
Settingsresponse = true;
orgName = jsonOrg.getString("orgName");
System.out.println("orgName" + orgName);
accessPointName = jsonOrg.getString("attendanceRecorderName");
System.out.println("accessPointName" + accessPointName);
lat = jsonOrg.getString("latitude");
System.out.println("lat" + lat);
longi = jsonOrg.getString("longitude");
System.out.println("longi" + longi);
floor = jsonOrg.getString("floor");
System.out.println("floor" + floor);
orgId = jsonOrg.getString("orgId");
System.out.println("orgId" + orgId);
}
else{
System.out.println("Data sent was erroneous");
Settingsresponse = false;
}
} catch (Exception e) {
System.err.print(e);
}
}
else if(JSONstate == true){
try {
[redacted]
}
else{
System.out.println("Data sent was erroneous");
Settingsresponse = false;
}
} catch (Exception e) {
System.err.print(e);
}
}
return null;
}
protected void onPostExecute(Void result){
if(JSONstate == false){
System.out.println("This piece of code is definitely being run");
setfields();
}
else if(JSONstate == true){
settestfields();
//This method does not run upon the completion of the JSON request, as it supposedly should
}
}
}
Once the JSONRequest has been completed, the 'onPostExecute' method doesn't run. I have been attempting to use this method so that a set of fields can be updated as soon as the request is complete, instead of having to set a definite wait time. Am I simply utilizing the code wrong? Or is there something I've missed?
You aren't overriding the correct method for onPostExecute.
You have:
protected void onPostExecute(Void result)
You need:
protected void onPostExecute(Object result)
Notice the third generic parameter you supplied was of type Object. That's the type that onPostExecute uses as an argument. So, the method signature for onPostExecute needs to accept an Object, not Void.
You should probably use a result type of boolean here rather than object, and remove the Json state class variable. This keeps your AsyncTask more flexible, and could allow you to display some indication the operation completed to the user after execution.
I have to say you codes in AsyncTask is nothing matches the point.
AsyncTask is designed as another thread running out from the UI-thread. So you should either use it as a inner class which is in a running UI-thread, then the onPostExecute() part can do something to show the result, or you as your codes, if you leave it as a stand alone class. You should design an interface, other class, like activity or fragment, which run new AsyncTask.execute() should implements that interface.
Also, java is not javascript. Your variables in doInBackground() is only limited in the function. So what you did in onPostExecute() will get nothing.
You should either use
JSONObject jsonOrg
as a class variable or you should return that at the end of doInBackground() and gain it back in onPostExecute()
After all, I suggest you look at the api document's example. Although it is a little complex, but it shows everything perfect.
try to use override methods
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
Log.i("in on ", "entered");
hideProgress();
}
As suggested by william the type should match with the override methods. I have edited the answer below
public class JSONDownloader extends AsyncTask<Object, Object, Object>
#Override
protected void onPostExecute(Object result) {
super.onPostExecute(result);
}

Categories

Resources