6

I have a trouble with my HttpsConnection on android.

First of all, no it is not a duplicate. I try almost all the solutions on SO, like changing the keep-alive option or the timeout ( and some of them indeed optimized a part of my code a little bit ) but it is still 5 to 10 times ( probably more ) slower on android than on iOS.

Sending a request to my server takes several seconds on android while it's almost instant on iOS and from a browser. I am sure that the server is not in cause. But it seems that getting the inputstream is terribly slow!

This line:

in=conn.getInputStream();

is the most delaying one, taking several seconds by itself.

My aim is to get a JSON from my server. My code is supposed to be technically as optimized as possible ( and it can probably help some people with HttpsConnection on the same time ):

protected String getContentUrl(String apiURL)
    {
        StringBuilder builder = new StringBuilder();
        String line=null;
        String result="";
        HttpsURLConnection conn= null;
        InputStream in= null;
        try {
            URL url;
            // get URL content
            url = new URL(apiURL);
            System.setProperty("http.keepAlive", "false");
            trustAllHosts();
            conn = (HttpsURLConnection) url.openConnection();
            conn.setHostnameVerifier(DO_NOT_VERIFY);
            conn.setRequestMethod("GET");
            conn.setRequestProperty(MainActivity.API_TOKEN, MainActivity.ENCRYPTED_TOKEN);
            conn.setRequestProperty("Connection", "close");
            conn.setConnectTimeout(1000);

            in=conn.getInputStream();


            // open the stream and put it into BufferedReader
            BufferedReader br = new BufferedReader(new InputStreamReader(in));

            while ((line=br.readLine())!= null) {
                builder.append(line);
            }

            result=builder.toString();
            //System.out.print(result);
            br.close();



        } catch (MalformedURLException e) {
            result=null;
        } catch (IOException e) {
            result=null;
        } catch (Exception e) {
            result=null;
        }
        finally {
            try {
                in.close();
            }catch(Exception e){}
            try {
                conn.disconnect();
            }catch(Exception e){}

            return result;
        }

    }

However, it keeps taking several seconds.

So I would like to know: is there a way to improve the speed of this API call? The problem is not the server or the JSON parsing but for sure the function above. Thanks a lot.

Virthuss
  • 3,142
  • 1
  • 21
  • 39
  • 2
    To the downvoters, have you even read the post? :( – Virthuss Nov 30 '15 at 10:53
  • I didn't downvote, but your question as it is is not easy to answer. There could be many reasons why your Android app is slower than the iOS. Are you sure the problem is not on the server ? Did you profile your app to find potential latency, bottleneck etc ? Alternatively, you can try using libraries like http://square.github.io/okhttp/ and compare the response times. – Philippe A Nov 30 '15 at 10:59
  • Hi Phillipe and thanks for your answer. Yes, I did all of that.I was of course at first suspecting the server to be slower than expected, or my parsing to be too slow. But getting the InputStream is definitively the slower part. – Virthuss Nov 30 '15 at 11:01
  • Then you can to search specifically why `HttpURLConnection.getInputStream()` is slow, here are 2 candidates http://stackoverflow.com/questions/1920623/sometimes-httpurlconnection-getinputstream-executes-too-slowly and http://stackoverflow.com/questions/17439802/httpurlconnection-getinputstream-very-slow – Philippe A Nov 30 '15 at 11:09
  • My case is Http**s**URLConnection however... And by the way, I already implemented the answer, see the keep-alive option as an example. Thanks however, it was a good start for me as well. – Virthuss Nov 30 '15 at 11:10
  • A problem like presented in your question requires more details: What models are you using to do those tests (post them)? What other apps are intercepting the network and the adapter? [Have you tuned details for HttpsURLConnection](http://stackoverflow.com/a/10117254/4903925)? Finally, is there a reason for you to focus on that connection class and not try fetchers like [Volley](http://developer.android.com/intl/es/training/volley/index.html) (personnal favorite over all, including OkHttp) – Bonatti Nov 30 '15 at 11:21
  • Ah yes I didn't read all the details of your code. Also you can try (if possible) to call the IP address of your server to save the DNS lookup... – Philippe A Nov 30 '15 at 11:21
  • @Bonatti My is https, not http. Also this method is supposed to be the fastest... Finally I tried the app on both my personnal phone and another one dedicated to the work, with only this app running. Saddly the result is the same. – Virthuss Nov 30 '15 at 11:26
  • @PhilippeA How can I achieve that? – Virthuss Nov 30 '15 at 11:27
  • What's the value of `apiURL` when you call the method ? – Philippe A Nov 30 '15 at 11:52
  • That's the URL of my own API call to one of my server. An https link. – Virthuss Nov 30 '15 at 16:11
  • @Virthuss even i face the same issue. Were you able to debug the cause ? – Shobhit Srivastava Mar 16 '16 at 09:22
  • @ShobhitSrivastava I didnt. As the result remained acceptable, I wasn't able to waste too much time on it... – Virthuss Mar 19 '16 at 21:00
  • @Virthuss did you find the problem? I'm facing the same issue - ios request is 3x-10x faster that the same on Android – sinek Oct 29 '18 at 14:24
  • @sinek I dont work on this project anymore, so sadly I can't tell... – Virthuss Sep 01 '20 at 07:17

0 Answers0