-1

I have an app that has to download a few MB (1MB - 10MB) of data from a server on app startup.

The problem is that the app:

  • doesn't show the main TextView before starting the download (the screen stays black)

  • is more or less unresponsive

Here is the code:

public class Main extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        DoIt();
    }

    private void DoIt() {

        HttpClient httpClient = new DefaultHttpClient();

        TextView tv = (TextView)findViewById(R.id.textView);
        tv.setText("Starting app...");

        try {        
            for (int i=1; i<100; i++) {
                HttpPost request2 = new HttpPost("http://192.168.1.12:3000/bytes/data" + i);            
                HttpResponse response2 = httpClient.execute(request2);

               // Do something with data. In some cases, it has to download 1MB data
            } 
        }

        // catch + finally ...
    }
}

How to prevent the app to be unresponsive?

Basj
  • 41,386
  • 99
  • 383
  • 673
  • 2
    You should be using a background thread to do network operations. https://developer.android.com/training/articles/perf-anr.html – codeMagic Dec 10 '17 at 21:19
  • @codeMagic Ok, but stlil, why isn't `tv.setText("Starting app...");` displaying? (It should be displayed before the download starts). – Basj Dec 10 '17 at 21:20
  • Do what @codeMagic says and you will have the text set. You could easily do it with AsynTask. – Cristian Dec 10 '17 at 21:26
  • `tv.setText("Starting app...");` isn't displaying because the user interface hasn't had a chance to update before you bogged down the main thread. – Sam Dec 10 '17 at 21:29
  • @Cristian any code example of how you would do it? – Basj Dec 10 '17 at 21:31
  • @Sam Ok, this makes sense. – Basj Dec 10 '17 at 21:31

1 Answers1

0

Your app become irresponsible because you are downloading files in main thread you should use background thread like this

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TextView tv = (TextView)findViewById(R.id.textView);
    tv.setText("Starting app...");

    new Thread(new Runnable() {
        @Override
        public void run() {
            DoIt();
        }
    }).start();

}

 private void DoIt() {

    HttpClient httpClient = new DefaultHttpClient();

    try {        
        for (int i=1; i<100; i++) {
            HttpPost request2 = new HttpPost("http://192.168.1.12:3000/bytes/data" + i);            
            HttpResponse response2 = httpClient.execute(request2);

           // Do something with data. In some cases, it has to download 1MB data
        } 
    }

    // catch + finally ...
}

I think this should work

Arvindo
  • 117
  • 1
  • 9
  • Thanks! I see it starting, I see the TextView with the text, and then I quickly get `Unfortunately, app has stopped`. Any idea? – Basj Dec 10 '17 at 21:49
  • I see thousands of lines in `adb logcat`, is there a way to display only those for my (crashing) app? – Basj Dec 10 '17 at 21:56
  • you have to see the exception which cause the stopping, read the logcat you will find _caused by_ and exception name, if you click on it you will find line number also – Arvindo Dec 10 '17 at 22:00
  • The problem is that `adb logcat` generates a flood of lines in realtime and I can't see the relevant lines. Isn't there a way to filter the messages related to a specific app (maybe this is directly possible in AndroidStudio, but I'm not using it) – Basj Dec 10 '17 at 22:01
  • you have to just carefully read the logcat and detect the error, it is the only solution – Arvindo Dec 10 '17 at 22:04