Apache HttpClientBuilder - IllegalArgumentException

3.4k views Asked by At

I get IllegalArgumentException when trying to build Apache HttpClient instance wit HttpClientBuilder:

private CloseableHttpClient delegate;

public DigestClient(String user, String pass) {
    Credentials credentials = new UsernamePasswordCredentials(user, pass);

    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, credentials);

    delegate = HttpClientBuilder.create()
            .setDefaultCredentialsProvider(credentialsProvider)
            .build();
}

what I get is:

 java.lang.IllegalArgumentException: Item may not be null
        at org.apache.http.util.Args.notNull(Args.java:48)
        at org.apache.http.config.RegistryBuilder.register(RegistryBuilder.java:58)
        at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:727)
        at com.example.app.data.net.DigestClient.<init>(DigestClient.java:51)

Any ideas why this happens?

Here's my gradle with HttpClient's version:

compile 'org.apache.httpcomponents:httpclient-android:4.3.5'
3

There are 3 answers

1
k_wisniewski On

I found a solution here:

http://javacave.blogspot.com/2014/08/androidapache-httpclient.html

For those who don't know Chinese:

I had to do:

SSLContext sslContext = SSLContexts.createSystemDefault();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);

and then

delegate = HttpClients.custom()
            .setDefaultCredentialsProvider(credentialsProvider)
            .setSSLSocketFactory(sslsf)
            .build();

At least it doesn't crash this way.

0
L. G. On

Upgrading httpclient-android version fixed it for me:

compile 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
1
Danijel Malic On
private CloseableHttpClient delegate;

public DigestClient(String user, String pass) {
    Credentials credentials = new UsernamePasswordCredentials(user, pass);

    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, credentials);

    delegate = HttpClientBuilder.create()
            .setDefaultCredentialsProvider(credentialsProvider)
            .build();
}