title: Retrieving HTTP response without blocking the main thread
body: <p>I am currently porting a networking library written in Objective-C to Java, and I need the functionality to make asynchronous HTTP requests to a server, without relying on any third-party libraries (so, no Apache dependencies). I am using <code>HTTPUrlConnection</code> to do this; however, as you know it is a synchronous process. </p>
  
  <p>I am trying to implement concurrency into this process using the <code>ExecutorService</code> and <code>Future&lt;&gt;</code> structures. I need to use <code>Callable</code> instead of <code>Runnable</code>, because I need to process the response message the server sends back to me. Here is a small overview of what I am doing:</p>
  
  <pre><code>ExecutorService executorService = Executors.newSingleThreadExecutor();
    Future&lt;String&gt; future = executorService.submit(new Callable&lt;String&gt;() {
       @Override
       public String call() throws Exception {
          return postRequest(reqMsg);
       }
    });
  </code></pre>
  
  <p>where reqMsg is the request message (in XML), and the postRequest method returns the response message as a String (also in XML). Now, in order to retrieve the return value of this Callable task, I need to use <code>future.get()</code>. However, this method is a blocking call, meaning it blocks the main thread until a response is available. This is not what I want. </p>
  
  <p>I am porting an iOS application to Android, and I am going to be using J2ObjC to have a cross-platform shared library between these platforms. In the iOS version (meaning the Obj-C version of the library), there are completion handlers that handle the results from HTTP request, and it is made asynchronously. Java, unfortunately, introduced this callback way of handling it in Java 8, with CompletableFuture. However, Android only supports CompletableFuture starting with API level 24. I want to be able to support API levels going back to Jelly Bean (API 16). And as far as I know, "for a JAR file to be compatible with Android, it can only reference classes available as part of Android and other classes implemented specifically in the JAR itself". You might suggest using AsyncTasks; however, I want to handle the concurrency of HTTP requests on the Java networking side, as this library will most likely be shared across two platforms. </p>
  
  <p>I have tried using Threads explicitly; however, as I researched it, I found out that in order to use Callables, you need to use ExecutorService and Future (plus, there seems to be some performance overheads related to creating Threads explicitly - though I think an overhead of &lt; 1000ms is acceptable). And again, just to reiterate (i.e. <strong>TL:DR</strong>):</p>
  
  <ul>
  <li><p>I don't want to rely on third party libraries, unless it's ABSOLUTELY necessary.</p></li>
  <li><p>I cannot use <code>CompletableFuture</code>, as I want my minimum API level to be 16.</p></li>
  <li><p>I don't want to handle this concurrency on the Android side with <code>AsyncTasks</code>, as originally, this is handled in the networking library with a <code>CompletionHandler</code> (Objective-C). </p></li>
  </ul>
  
  <p>Is there a way to use <code>Future.get()</code> without blocking? (A kind reminder that a while loop checking <code>Future.isDone()</code> also blocks the main thread).</p>