
spring-android-auth ClientHttpRequest
问题H1
使用spring-android-auth时,其使用的RestTemplate是依赖包spring-android-rest-template中的,而是spring-web-mvc中的。该RestTemplate使用http request默认的逻辑如下
bash
private static final boolean HTTP_COMPONENTS_AVAILABLE = ClassUtils.isPresent("org.apache.http.client.HttpClient", ClientHttpRequestFactory.class.getClassLoader());if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {this.requestFactory = new SimpleClientHttpRequestFactory();} else {this.requestFactory = new HttpComponentsClientHttpRequestFactory();}
使用spring-android-auth的主要目的是使用其依赖的spring-social,其中的OAuth1Template,OAuth2Template等使用的RestTempalte中使用的http request是ClientHttpRequestFactory。
bash
public static ClientHttpRequestFactory getRequestFactory() {Properties properties = System.getProperties();String proxyHost = properties.getProperty("http.proxyHost");int proxyPort = properties.containsKey("http.proxyPort") ? Integer.valueOf(properties.getProperty("http.proxyPort")) : 80;if (HTTP_COMPONENTS_AVAILABLE) {return HttpComponentsClientRequestFactoryCreator.createRequestFactory(proxyHost, proxyPort);} else {SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();if (proxyHost != null) {requestFactory.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)));}return requestFactory;}}
由于Android SDK中带有HttpClient这个类的,但是又不包含创建的实例对象CloseableHttpClient,实际会报错ClassNotFound。
解决H1
spring-android的构建脚本中对于spring-android-rest-template模块的依赖
bash
project("spring-android-rest-template") {description = "Spring for Android Rest Template"ext.mavenTestDir = "../test/spring-android-rest-template-test/pom.xml"dependencies {provided("com.google.android:android:$androidVersion")compile(project(":spring-android-core"))optional("org.apache.httpcomponents:httpclient-android:$httpclientVersion")optional("com.squareup.okhttp:okhttp-urlconnection:$okHttpVersion")optional("org.codehaus.jackson:jackson-mapper-asl:$jacksonVersion")optional("com.fasterxml.jackson.core:jackson-databind:$jackson2Version")optional("com.google.code.gson:gson:$gsonVersion")optional("org.simpleframework:simple-xml:$simpleXmlVersion") { dep ->transitive = false}}}
httpclient-android是一个optional的依赖,所以使用HttpClient需要显示的指定依赖。
bash
compile group: 'org.apache.httpcomponents', name: 'httpclient-android', version:'4.3.3'
评论
新的评论
上一篇
Error: duplicate files during packaging of APK
错误 Android项目构建时报如下错 原因 打包apk文件时,资源文件冲突。 解决 Maven Gradle 总结 Android官方文档 对构建过程中打包apk的描述 All non-compiled resources (such as images), compiled…
下一篇
SwipeRefreshLayout
SwipeRefreshLayout 可以用于通过下滑的手势刷新View内容的情况。使用起来很简单,用SwipeRefreshLayout包裹需要刷新的View即可。 初始化View的Activity应该注册一个 OnRefreshListener 来处理手势完成时需要执行的(…
