Eduardo
Eduardo

Reputation: 7141

Efficiently making multiple GET requests to the same url in Java

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;
    }
}

Upvotes: 0

Views: 2261

Answers (1)

Kostas Kryptos
Kostas Kryptos

Reputation: 4111

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.

Upvotes: 2

Related Questions