0

I need to make multiple GET requests to the same URL but with different queries. I will be doing this on a mobile device (Android) so I need to optimise as much as possible. I learnt from watching an Android web seminar by Google that it takes ~200ms to connect to a server and there's also various other delays involved with making data calls. I'm just wondering if theres a way I can optimise the process of making multiple requests to the same URL to avoid some of these delays?

I have been using the below method so far but I have been calling it 6 times, one for each GET request.

//Make a GET request to url with headers.
//The function returns the contents of the retrieved file

public String getRequest(String url, String query, Map<String, List<String>> headers) throws IOException{
    String getUrl = url + "?" + query;
    BufferedInputStream bis = null;
    try {
        connection = new URL(url + "?" + query).openConnection();
        for(Map.Entry<String, List<String>> h : headers.entrySet()){
            for(String s : h.getValue()){
                connection.addRequestProperty(h.getKey(), s);
            }
        }

        bis = new BufferedInputStream(connection.getInputStream());
        StringBuilder builder = new StringBuilder();
        int byteRead;
        while ((byteRead = bis.read()) != -1)
            builder.append((char) byteRead);

        bis.close();
        return builder.toString();
    } catch (MalformedURLException e) {
        throw e;
    } catch (IOException e) {
        throw e;
    }
}
Eduardo
  • 6,900
  • 17
  • 77
  • 121

1 Answers1

2

If for every request you expect another result and you cannot combine requests by adding more than one GET variables in the same request then you cannot avoid the 6 calls.

However you can use multiple Threads to simultaneously run your requests. You may use a Thread Pool approach using the native ExecutorService in Java. I would propose you to use an ExecutorCompletionService to run your requests. As the processing time is not CPU-bounded, but network-bounded, you may use more Threads than your current CPUs.

For instance, in some of my projects I use 10+, sometimes 50+ Threads (in a Thread Pool) to simultaneously retrieve URL data, even though I only have 4 CPU cores.

Kostas Kryptos
  • 4,081
  • 2
  • 23
  • 24
  • Is there no way i can open a connection to the server and use that same connection for each request? So i dont have to reconnect to the server then disconnect each time – Eduardo May 10 '14 at 20:12
  • see the reply on this post: HttpURLConnection will reuse connections if it can! http://stackoverflow.com/questions/5459162/tcp-connection-is-not-reused-for-http-requests-with-httpurlconnection – Kostas Kryptos May 10 '14 at 20:24