0

I am trying to download data in Async Task. But there has been a delay for around 10 to 12 seconds when that activity is called for the first time. When application is closed and opened immediately there has no been any delay. I observed that there has been delay near function getInputStream().

I have seen other suggestions in SO saying to use.

urlConnection.setReadTimeout(time);

But there has been no use.

I also tried adding this line as suggested in https://stackoverflow.com/a/1921530/7512939:

  System.setProperty("http.keepAlive", "false");

Here is my code:

  public class GetAllStationsAndStopsAsyncTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... objects) {
        String urlString = getUrl();
        HttpURLConnection urlConnection = null;

        BufferedReader bufferedReader;
        String line;
        InputStream inputStream;
        StringBuilder json_result = new StringBuilder();
        try {
            System.setProperty("http.keepAlive", "false");
            URL url = new URL(urlString);
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setReadTimeout(2000);
            urlConnection.setConnectTimeout(2000);
            urlConnection.setRequestMethod("GET");
            Log.v(TAG, "before");
            inputStream = new BufferedInputStream(urlConnection.getInputStream());
            Log.v(TAG, "before 1");
            bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

            while ((line = bufferedReader.readLine()) != null) {
                json_result.append(line);
            }
            Log.v(TAG, "before 2");

        } catch (Exception e) {
            Log.e(TAG, "before first error"+e.getLocalizedMessage());
            getActivity().runOnUiThread(new Runnable() {
                public void run() {
                    new GetAllStationsAndStopsAsyncTask().execute();
                }
            });
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
        }
        return null;
    }

Please let me know how to avoid this huge delay. TIA

Community
  • 1
  • 1
Sindhu
  • 421
  • 1
  • 6
  • 16
  • Where are you initially starting this task? I see it executing inside `doInBackground` but it should be somewhere else too? – Derek Apr 10 '17 at 16:41
  • @Darek Baxter I am actually calling from another class. I did not add the code of that class. – Sindhu Apr 10 '17 at 16:48
  • How big is the data that you are loading? If the delay is coming from the `inputStream` it sounds like it's getting a lot of data – Derek Apr 10 '17 at 16:57
  • @Darek Baxter Yes the data that is coming is actually very large. Is it the reason for delay? Is there any way to overcome it? – Sindhu Apr 10 '17 at 16:59
  • What is the data you are getting look like? – Derek Apr 10 '17 at 17:33
  • Also check the size `int size = urlConnection.getContentLength();` and see what it is – Derek Apr 10 '17 at 17:34
  • Sorry for the late reply. I tried urlConnection.getContentLength() which is returning -1. I think that header is not set by the server. As you have seen in code I have set readTimeout to 2 sec. Sometimes in 2sec it is being successfully completed and other times it is taking more than 2 sec, so it is going to catch block – Sindhu Apr 11 '17 at 07:52
  • If the server is sometimes slow to respond or transmitting large data takes time, there isn't much (or anything) that can be done on the client side to speed things up. The content length [isn't available due to Android's default behavior](http://stackoverflow.com/questions/32988325/android-getcontentlength-always-return-1-when-downloading-apk-file#32990226). – Markus Kauppinen Apr 12 '17 at 08:46

0 Answers0