看到网上说android 6.0 系统源码已删除HttpClient的相关类,volley要兼容6.0
解决方法如下:
1.eclipse:
上面的jar包在:sdk\platforms\android-23\optional下(需要下载android 6.0的SDK)
android {
useLibrary 'org.apache.http.legacy'
}
后来我在android studio项目中怀着研究的心态尝试删除这句配置,编译可以通过,在8.0系统上安装可以正常运行。
于是我反编译了自己的apk,看看是否编译的时候就已将相关类打包进apk了呢。
结果证明并没有打包进apk;
将compileSdkVersion设置为22是可以找到org.apache.http下面的类的如(org.apache.http.impl.client.DefaultHttpClient),如果将compileSdkVersion >= 23编译器则会报错,找不到该类。
于是我便通过反射获取org.apache.http.impl.client.DefaultHttpClient,结果在8.0系统上可以找到。于是我就在8.0系统源码中搜索DefaultHttpClient,结果搜到了。路径如下
android-8.0.0_r1\external\apache-http\src\org\apache\http\impl\client.DefaultHttpClient.java
得出结论:
6.0只是sdk中删除了org.apache.http相关类,系统中依然保留(反射可以获取到)。
所以volley无需添加任何代码可在6.0以上系统运行。
如果自己项目中编写的代码需要用的org.apache.http相关类,则需添加配置
android {
useLibrary 'org.apache.http.legacy'
}
有了这个配置,编译器会将
sdk\platforms\android-23\optional\org.apache.http.legacy.jar 添加外部依赖,编译器就不会报错。