我一直在我的Android应用程序中使用内置的android httpclient和Springs Android Resttemplate(1.0.0.M4) . 我正在使用Basic Auth进行身份验证 .
我在提出请求时需要设置一些东西,最重要的是凭据字符集 . 默认为US-Ascii,除非我将其设置为ISO-8859,否则北欧字符不起作用 .
我使用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", )
我现在正在尝试升级我的库,并且还摆脱了内置的httpclient依赖项 .
所以,我正在升级到resttemplate 2.0,并导入了apache的httpclient-android . 我正在使用Maven,所以我的依赖项是:
<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>
我很难按照自己的意愿完成这项工作......
-
以上任何一个 jar 都没有HttpClient类 . 由于Android版已被弃用,我不想依赖那个 .
-
根据文档,Basic Auth应按照以下代码执行 . 但我找不到任何地方设置凭证字符集!
HttpAuthentication authHeader = new HttpBasicAuthentication(msisdn,password); HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.setAuthorization(authHeader); HttpEntity requestEntity = new HttpEntity(requestHeaders);
如果有人可以帮助我,那就太好了 .
1 回答
如果您想在Android上使用已弃用的Apache HTTP客户端,可以通过Android Gradle插件的
useLibrary
声明将其添加到构建中:你可以在这里找到更多信息:https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-apache-http-client
不幸的是,我不认为有一种方法可以自己添加HTTP客户端 .
UPDATE: 向
RestTemplate
项目提交了一个问题,允许独立的Apache HTTP客户端:https://github.com/spring-projects/spring-android/issues/31