今天在測試android api 28上的兼容問題時(shí),出現(xiàn)2個的異常:
Didn't find class "org.apache.http.entity.StringEntity" on path
在過去的 Android 6 中,取消了對 Apache HTTP 客戶端的支持, 而從 Android 9 開始官方又過分了,默認(rèn)情況下該內(nèi)容庫已從 bootclasspath 中移除且不可用于應(yīng)用,原因未知,望告知。
所以 Android 6.0 以上時(shí),如果想使用org.apache.http的話,可以在gradle中android配置下寫入:
useLibrary 'org.apache.http.legacy'
在Android 9以上時(shí),如果還要繼續(xù)使用org.apache.http的話,可以在AndroidManifest.xml中application節(jié)點(diǎn)下面寫入:
<uses-library
android:name="org.apache.http.legacy"
android:required="false" />
而第二個異常:
onFailurejava.io.IOException: Cleartext traffic not permitted
其原因是在Android 9以上系統(tǒng)中,默認(rèn)情況下啟用網(wǎng)絡(luò)傳輸層安全協(xié)議 (TLS),是不支持HTTP明文請求的,也就是支持HTTPS了。
所以解決方法是:
1.改成https請求
2.修改targetSdkVersion為28以下
3.向別人學(xué)習(xí)更全面的解決方案