I'm trying to implement a REST communication on Android with the Apache HttpAsyncClient (version 4.0-beta1, which is the newest at the time of writing this post).
My problem is, that I cannot get any request out, because of a MethodNotFound error which comes from WITHIN the framework.
The detailed stacktrace is (after the presented rows, the next one comes from my own code):
java.lang.NoSuchMethodError: org.apache.http.client.utils.URIUtils.extractHost
at org.apache.http.impl.nio.client.AbstractHttpAsyncClient.determineTarget(AbstractHttpAsyncClient.java:594)
at org.apache.http.impl.nio.client.AbstractHttpAsyncClient.execute(AbstractHttpAsyncClient.java:578)
at org.apache.http.impl.nio.client.AbstractHttpAsyncClient.execute(AbstractHttpAsyncClient.java:569)
What I did is the following:
private static final String SERVER_SCHEME = "http";
private static final String SERVER_HOST = "localhost";
private static final int SERVER_PORT = 3000;
private static final String SERVER_BASE_PATH = "/api/v1";
private static final String PATH_LOGIN = "/login";
private DefaultHttpAsyncClient http_client;
public void test(String email, String password, ConnectionManagerCallback callback) {
this.http_client = new DefaultHttpAsyncClient();
this.http_client.start();
this.http_client.getCredentialsProvider().setCredentials(new AuthScope(SERVER_HOST, SERVER_PORT), new UsernamePasswordCredentials(email, password));
HttpPost request = new HttpPost(SERVER_SCHEME + "://" + SERVER_HOST + ":" + SERVER_PORT + SERVER_BASE_PATH + PATH_LOGIN);
this.http_client.execute(request, new Callback(REQUEST_LOGIN, callback));
}
I have removed the Exception Handling for the sake of the example. The problem is, that I get the exception I described above. So since it's a beta version I thought of using a different constructor for the HttpPost. But even when I give an URI Object to the constructur it tries to call the extractHost method and crashes.
Does anyone have any help for me on this one? I just added the JARs by the way and did not compile the library myself.
Apache HttpAsyncClient is based on a low level HTTP transport library called HttpCore. Google Android ships with an extremely outdated version of Apache HttpClient which is also based on the same library. HttpAsyncClient requires a newer version of HttpCore that the one shipped with Android. However, classes from the older version get picked up by the classloader first causing NoSuchMethodError exception whenever an attempt is made to call a method from the newer version.
Unfortunately there the only solution to this problem is moving all HttpAsyncClient code and its dependencies to a different name space.
Update Nov 2020
Upgrade to Apache HttpClient 5.x