0

I have been using the build-in android httpclient and Springs Android Resttemplate (1.0.0.M4) in my Android app. I'm authenticating with Basic Auth.

I have a couple of things i need to set when making requests, most importantly credentials charset. Default is US-Ascii and nordic characters doesn't work unless i set it to ISO-8859.

I did that using the HttpClient:

    HttpClient httpClient = new HttpClient();
    Credentials defaultcreds = new UsernamePasswordCredentials(msisdn, password);
    httpClient.getState().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM), defaultcreds);
    httpClient.getParams().setSoTimeout(prefs.getServerTimeout());
    httpClient.getParams().setConnectionManagerTimeout(3000);
    httpClient.getParams().setContentCharset("UTF-8");
    httpClient.getParams().setCredentialCharset("ISO-8859-1", )

I'm now trying to upgrade my libraries, and also get rid of the built-in httpclient dependency.

SO, i'm upgrading to resttemplate 2.0, and have imported apache's httpclient-android. I'm using Maven, so my dependencies are:

<dependency>
        <groupId>org.springframework.android</groupId>
        <artifactId>spring-android-rest-template</artifactId>
        <version>2.0.0.M3</version>
        <!--version>1.0.0.M4</version-->
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient-android</artifactId>
        <version>4.3.5.1</version>
    </dependency>

I have a hard time making this work as i want...

  1. Neither of the jars above have a HttpClient class anywhere. Since the Android one is deprecated, i don't want to rely on that one.

  2. According to the docs, Basic Auth should be performed as per the below code. But i cannot find anywhere to set the credentials charset!

    HttpAuthentication authHeader = new HttpBasicAuthentication(msisdn, password); HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.setAuthorization(authHeader); HttpEntity requestEntity = new HttpEntity(requestHeaders);

It would be great if someone could help me out.

2
  • cz.msebera.android:httpclient:4.4.1.1 has cz.msebera.android.httpclient.client.HttpClient. That's what I used in this book sample and this NetCipher code. With regards to basic auth charsets, I have no idea. Commented Jul 29, 2016 at 10:37
  • Right, thanks. Unfortunately that doesn't work with Spring's RestClient, so i'd have to figure out another way to to the rest->object work and test it. Commented Aug 1, 2016 at 8:47

1 Answer 1

0

If you want to use the deprecated Apache HTTP client on Android you can add it to the build by useLibrary declaration of Android Gradle plugin:

android {
    useLibrary 'org.apache.http.legacy'
}

You can find more info here: https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-apache-http-client

Unfortunately I don't think there is a way how to add the HTTP client to the on your own.

UPDATE: There is an issue filed to RestTemplate project to allow standalone Apache HTTP client: https://github.com/spring-projects/spring-android/issues/31

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.