最近在做项目,在Android9.0以下运行没啥问题,然后我手机升级到9.0之后,竟然很多地方无法联网?!
例如使用glide加载图片,im的登陆,推送的注册,甚至某些接口直接无法访问。曾一度以为不小心删除什么神秘代码,可是在其他手机上完全正常。
然后进行调试,出现了一片红色异常,仔细一看异常信息都和“HTTP”有关,查阅了一下资料,发现在Android9.0的机器上,默认不支持http的访问,所有网络访问必须使用https。当然Android早几个版本已经支持https了,只不过在9.0上默认使用https不支持http。除了一片红之外,我还注意到一个系统抛出的异常:
注意到了一个系统抛出的异常:
Cause (1 of 1): class java.io.IOException: Cleartext HTTP traffic to xxxx.xxxx.xxxx not permitted
这个就说的很明显了。于是解决方案如下:
在清单文件增加代码:
android:usesCleartextTraffic="true"
然后重新运行,是解决了服务器httpj接口访问和图片加载的问题,可是im还是登录不上去,推送也注册失败。
然后我们仔细看一异常:
2018-12-06 11:06:15.338 16331-16591/com.mengyouyue.mengyy E/XgStat: java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/params/BasicHttpParams;
at com.tencent.android.tpush.stat.f.<init>(ProGuard:63)
at com.tencent.android.tpush.stat.f.b(ProGuard:113)
at com.tencent.android.tpush.stat.i.run(ProGuard:229)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.os.HandlerThread.run(HandlerThread.java:65)
Caused by: java.lang.ClassNotFoundException: Didn't find class "org.apache.http.params.BasicHttpParams" on path: DexPathList[[zip file "/data/app/com.mengyouyue.mengyy-qPGAR6mRwBBJ-w3WuxbdcQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.mengyouyue.mengyy-qPGAR6mRwBBJ-w3WuxbdcQ==/lib/arm, /data/app/com.mengyouyue.mengyy-qPGAR6mRwBBJ-w3WuxbdcQ==/base.apk!/lib/armeabi-v7a, /system/lib, /vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:171)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at com.tencent.android.tpush.stat.f.<init>(ProGuard:63)
at com.tencent.android.tpush.stat.f.b(ProGuard:113)
at com.tencent.android.tpush.stat.i.run(ProGuard:229)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.os.HandlerThread.run(HandlerThread.java:65)
2018-12-06 11:06:15.349 16331-16591/com.mengyouyue.mengyy E/XgStat: java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/client/methods/HttpPost;
at com.tencent.android.tpush.stat.f.a(ProGuard:197)
at com.tencent.android.tpush.stat.f.b(ProGuard:371)
at com.tencent.android.tpush.stat.f.a(ProGuard:383)
at com.tencent.android.tpush.stat.h.a(ProGuard:684)
at com.tencent.android.tpush.stat.n.run(ProGuard:376)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.os.HandlerThread.run(HandlerThread.java:65)
Caused by: java.lang.ClassNotFoundException: Didn't find class "org.apache.http.client.methods.HttpPost" on path: DexPathList[[zip file "/data/app/com.mengyouyue.mengyy-qPGAR6mRwBBJ-w3WuxbdcQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.mengyouyue.mengyy-qPGAR6mRwBBJ-w3WuxbdcQ==/lib/arm, /data/app/com.mengyouyue.mengyy-qPGAR6mRwBBJ-w3WuxbdcQ==/base.apk!/lib/armeabi-v7a, /system/lib, /vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:171)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at com.tencent.android.tpush.stat.f.a(ProGuard:197)
at com.tencent.android.tpush.stat.f.b(ProGuard:371)
at com.tencent.android.tpush.stat.f.a(ProGuard:383)
at com.tencent.android.tpush.stat.h.a(ProGuard:684)
at com.tencent.android.tpush.stat.n.run(ProGuard:376)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.os.HandlerThread.run(HandlerThread.java:65)
2018-12-06 11:06:15.349 16331-16591/com.mengyouyue.mengyy D/XgStat: [XgStat(796): ProGuard:706] - store event size:1
2018-12-06 11:06:15.364 16331-16591/com.mengyouyue.mengyy E/XgStat: java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/client/methods/HttpPost;
at com.tencent.android.tpush.stat.f.a(ProGuard:197)
at com.tencent.android.tpush.stat.f.b(ProGuard:371)
at com.tencent.android.tpush.stat.h.e(ProGuard:757)
at com.tencent.android.tpush.stat.h.b(ProGuard:916)
at com.tencent.android.tpush.stat.s.run(ProGuard:651)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.os.HandlerThread.run(HandlerThread.java:65)
Caused by: java.lang.ClassNotFoundException: Didn't find class "org.apache.http.client.methods.HttpPost" on path: DexPathList[[zip file "/data/app/com.mengyouyue.mengyy-qPGAR6mRwBBJ-w3WuxbdcQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.mengyouyue.mengyy-qPGAR6mRwBBJ-w3WuxbdcQ==/lib/arm, /data/app/com.mengyouyue.mengyy-qPGAR6mRwBBJ-w3WuxbdcQ==/base.apk!/lib/armeabi-v7a, /system/lib, /vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:171)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at com.tencent.android.tpush.stat.f.a(ProGuard:197)
at com.tencent.android.tpush.stat.f.b(ProGuard:371)
at com.tencent.android.tpush.stat.h.e(ProGuard:757)
at com.tencent.android.tpush.stat.h.b(ProGuard:916)
at com.tencent.android.tpush.stat.s.run(ProGuard:651)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.os.HandlerThread.run(HandlerThread.java:65)
2018-12-06 11:06:15.364 16331-16591/com.mengyouyue.mengyy E/XgStat: java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
at android.app.SharedPreferencesImpl.getInt(SharedPreferencesImpl.java:302)
at com.tencent.android.tpush.stat.h.d(ProGuard:739)
at com.tencent.android.tpush.stat.k.b(ProGuard:766)
at com.tencent.android.tpush.stat.f.a(ProGuard:345)
at com.tencent.android.tpush.stat.f.b(ProGuard:371)
at com.tencent.android.tpush.stat.h.e(ProGuard:757)
at com.tencent.android.tpush.stat.h.b(ProGuard:916)
at com.tencent.android.tpush.stat.s.run(ProGuard:651)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.os.HandlerThread.run(HandlerThread.java:65)
我滴乖乖,腾讯推送找不到http.client的类了。android9.0默认不支持这货了,没办法,只能在清单再加一句代码:
<uses-library android:name="org.apache.http.legacy" android:required="false" />
好了,重新运行解决了,可以继续浪了。
当然我觉得这并不是最优的解决方案,最根本的做法是使用https进行接口访问,毕竟涉及数据的安全性。当然了,这需要服务器的支持。还有坑爹的第三方sdk,希望他们能尽快适配高版本的android.
仅作为记录异常---2018-12-06 10:35