0

I've got a question about speeding up the loading of a text file. There may be a similar thread already, but it's pretty hard to find.

I've got an interesting set-up. On launch the app contacts my server for the latest data. This is basically a long text file containing some app info as well as the latest data. This works quite well. There is a service with an ASyncTask that does this. I've included the code for the task below.

I've enabled some caching on the server and the file loads pretty quick in my browser. However, it takes longer (like 30+ seconds) from my app, even on great connections. I've enabled GZIP on my server, and that makes loading in the browser even quicker (<1 second). However, my app is still slow. Considering this happens on the first start-up of the app it's quite a bad experience to have to wait this long.

Are there any more things I can do to speed up this process from the app?

Thanks in advance

(Code below)

    private class DownloadData extends AsyncTask<String, Void, String> { 

          @Override
          protected String doInBackground(String... params) { 
              String result = ""; 
              try { 
                // Create a URL for the desired page 
                URL url = new URL("LINK HERE"); 

                // Read all the text returned by the server 
                BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); 
                String str;  
                while ((str = in.readLine()) != null) { 
                    result += str; 
                } 
                in.close(); 
            } catch (MalformedURLException e) { 
            } catch (IOException e) { 
            } 
              return result; 
          }       

          @Override
          protected void onPostExecute(String result) { 
            // Result processed here. It takes 30 seconds to reach this point, so no delay is happening here
          } 

          @Override
          protected void onPreExecute() { 
             // Some UI Things
          } 

          @Override
          protected void onProgressUpdate(Void... values) { 
          } 
    } 
Vic V
  • 1,080
  • 2
  • 12
  • 31
  • 1
    This while loop is making your downloading slow: `while ((str = in.readLine()) != null) { result += str; } ` While by applying the `byte[] buffer = new byte[1024];` and read as much bytes, will solve this. Make buffer size accordingly. – jitain sharma Apr 22 '14 at 14:42
  • Interesting, I will try this as soon as I'm able. If this fixes it I'll let you know so I can mark the answer as completed. Thanks for taking a look. – Vic V Apr 22 '14 at 14:50

1 Answers1

1

I am not sure that it it is the biggest problem in your code but you can try to optimize by not using a string class but char[].

 char[] resultTab=new char[Integer.MAX_VALUE];
 int i=0;
 while ((resultTab[i] = in.read()) != -1) { 
          i++;
 } 
 String result = String.copyValueOf(resultTab, 0, i);

In place of Integer.MAX_VALUE, you put the lenght estimate of your string. (a bit more, if your not sure let it)

If you are using a string your aplication have to alocate new memory when your text became bigger, with a char [], it will alocate memory only once.

Tell me if it's work, i am interest by the result.

Eliott Roynette
  • 716
  • 8
  • 21
  • Thanks for explaining what and why. Will test results within the hour. Just for my information, would this work if the file length is variable, or will it stop working after the specified amount of characters? – Vic V Apr 22 '14 at 15:22
  • Yes it will stop to work when you have more than 10000 caracters. However, I don't think that it will be a big problem if you put a BIG number of char (like Integer.MAXVALUE and Integer.MAXVALUE is a BIG number, i don't think your string is bigger than that :) ) – Eliott Roynette Apr 22 '14 at 17:02
  • Cheers, but it seems to crash (it does so quickly though, so there's that :D) Here's my implementation: http://pastebin.com/qnxxk0Ff Here's the logcat: http://pastebin.com/0mRxu0fi It seems to be the same when I change the `MAXVALUE` to the exact numbers of characters (+1), which is in this case 612K. – Vic V Apr 22 '14 at 17:41
  • Inspired by both answers, I ended up with this question: http://stackoverflow.com/questions/2492076/android-reading-from-an-input-stream-efficiently. The app now loads in <1 second! Thanks for pointing out the error in my code! As an amateur, I never would have guessed something so small could make so much difference in loading times. – Vic V Apr 22 '14 at 18:43