title: API call far faster on iOs and browser than on android
body: <p>I have a trouble with my <code>HttpsConnection</code> on android.</p>
  
  <p><em>First of all, no it is not a duplicate. I try almost all the solutions on SO, like changing the <code>keep-alive</code> option or the <code>timeout</code> ( 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.</em></p>
  
  <p>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!</p>
  
  <p><strong>This line:</strong></p>
  
  <pre><code>in=conn.getInputStream();
  </code></pre>
  
  <p><strong>is the most delaying one, taking several seconds by itself</strong>. </p>
  
  <p>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 <code>HttpsConnection</code> on the same time ):</p>
  
  <pre><code>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;
          }
  
      }
  </code></pre>
  
  <p>However, it keeps taking several seconds.</p>
  
  <p>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.</p>