1

In my app I'm getting some TEXT from a Website by ion library and I'm using a progressBar to show the progress of downloading that data.

But my main issue is that in onComplete I implemented some methods that work with the TEXT I've got from the website these are some cyclic methdods that use lot's of for and other stuff and actually my progressBar at the beginning works properly, but when it's complete downloading the data the progressBar just sticks until all the methods are complete in onComplete().

I would that the progressBar runs anyway and doesn't get stuck without removing methods from onComplete() is it possible?

Here is my function where I use ion library that I invoke in the onClick() event:

private void getHTML(){
    progressDialog = new SpotsDialog(MainActivity.this, R.style.Custom);
    progressDialog.show();
    Ion.with(getApplicationContext())
            .load("IP")
            .asString()
            .setCallback(new FutureCallback<String>() {
                @SuppressLint("SetTextI18n")
                @Override
                public void onCompleted(Exception e, String result) {
                    htmlresultart = result;
                    htmlresultart = htmlresultart.replace("</td>", "\n");
                    getTable();
                    getHeader();
                    getBody();
                    progressDialog.cancel();
                }
            });

}
John K
  • 371
  • 5
  • 26

1 Answers1

1

If the methods called in the onCompleted() callback take too much to execute then they need to be run on a background thread. In this situation it doesn't make sense to use a callback with Ion, instead you should synchronously fetch the data and after this do all the other tasks, all on a single background thread, something like below:

private void getHTML(){
    progressDialog = new SpotsDialog(MainActivity.this, R.style.Custom);
    progressDialog.show();
    // start a background thread
    ExecutorService executor = Executors.newSingleThreadExecutor();
    executor.execute(new Runnable() {
         @Override
         public void run() {
            String htmlresultart = null;
            try {  
                 String htmlresultart = Ion.with(getApplicationContext())
                   .load("IP")
                   .asString()
                   .get(); 
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
            // if htmlresultart is null at this point, an exception occured and the string couldn't be fetched so you most likely should abort the following processing.
            if (htmlresultart != null) {
                htmlresultart = htmlresultart.replace("</td>", "\n");
                getTable();
                getHeader();
                getBody();
                progressDialog.cancel();
            }
         }
    });        
}
user
  • 86,916
  • 18
  • 197
  • 190
  • 1
    Actually in .get(); Android Studio is saying Unhandled exception, how can i fix it? It also suggest to Surround with try catch should i try with it? – John K May 31 '18 at 13:17
  • 2
    @I.Mytyuk That get() method is declared to throw those exceptions in some cases. You need to either wrap it with a try/catch block or make getHTML() to throw those exception as well. Check the edit I made. – user May 31 '18 at 13:21
  • 1
    @I.Mytyuk Are you finishing or going to another activity from MainActivity after you call getHTML()? – user May 31 '18 at 13:31
  • Ok actually with your updated answer it's don't crash anymore but it's the progressAlert doesn't stop and it seems it doesen't even execute getTable() and other methods – John K May 31 '18 at 13:36
  • No actually i'm just populating a database in getBody by converting the data got from ion, so by clicking a button i invoke getHTML method – John K May 31 '18 at 13:38
  • @I.Mytyuk If methods like getTable() don't get called it seems the htmlresultart is null because an exception was thrown above. Check the Logcat to see why the call to fetch the String fails. – user May 31 '18 at 13:40
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/172168/discussion-between-i-mytyuk-and-luksprog). – John K May 31 '18 at 13:44