{"id":1242,"date":"2023-03-18T09:33:45","date_gmt":"2023-03-18T01:33:45","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1242"},"modified":"2023-04-29T09:31:08","modified_gmt":"2023-04-29T01:31:08","slug":"android-p-adaptation-record","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/18\/android-p-adaptation-record\/","title":{"rendered":"Android P \u9002\u914d\u8e29\u5751\u8bb0\u5f55"},"content":{"rendered":"<h3>\u4f7f\u7528 org.apache.http.legacy \u5e93\u5728Android 9.0\u4e0a\u8fd0\u884c\u51fa\u73b0\u5d29\u6e83<\/h3>\n<h4>\u65e5\u5fd7\u5185\u5bb9<\/h4>\n<pre><code>java.lang.NoClassDefFoundError: Failed resolution of: Lorg\/apache\/http\/message\/BasicNameValuePair<\/code><\/pre>\n<p><!-- more --><\/p>\n<h4>\u95ee\u9898\u5206\u6790<\/h4>\n<p>\u51fa\u73b0\u6b64\u95ee\u9898\u4e3b\u8981\u662fapache\u7684http\u5e93\u5728Android 9.0\u5220\u9664\u5bfc\u81f4\u7684\uff0c\u4e0d\u8981\u5728Android 9.0\u4e2d\u4f7f\u7528HttpClient\u8bbf\u95ee\u7f51\u7edc\uff0c\u66ff\u6362\u6210HttpURLConnection\u6216\u8005OKHttp\u3002<\/p>\n<h4>\u89e3\u51b3\u65b9\u6848<\/h4>\n<p>\u5982\u679c\u662f\u5f15\u7528\u7684\u7b2c\u4e09\u65b9\u5e93\u4f7f\u7528HttpClient\u5bfc\u81f4\u65e0\u6cd5\u56de\u907f\uff0c\u5219<\/p>\n<p>\uff081\uff09\u5728build.gradle\u6587\u4ef6\u4e2d\u58f0\u660e\u7f16\u8bd1\u65f6\u4f9d\u8d56<\/p>\n<pre><code class=\"language-java\">android {\n    useLibrary &#039;org.apache.http.legacy&#039;\n}<\/code><\/pre>\n<p>\uff082\uff09\u5728AndroidManifest.xml\u6587\u4ef6\u7684application\u6807\u7b7e\u91cc\u9762\u6dfb\u52a0<\/p>\n<pre><code class=\"language-xml\">&lt;uses-libraryandroid:name=&quot;org.apache.http.legacy&quot;android:required=&quot;false&quot;\/&gt;<\/code><\/pre>\n<p>\u53c2\u8003\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/www.cnblogs.com\/renhui\/p\/9798335.html\">https:\/\/www.cnblogs.com\/renhui\/p\/9798335.html<\/a><br \/>\n\u53c2\u8003\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/stackoverflow.com\/questions\/50446148\/how-to-use-apache-http-on-android-p\">https:\/\/stackoverflow.com\/questions\/50446148\/how-to-use-apache-http-on-android-p<\/a><br \/>\n\u53c2\u8003\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/juejin.im\/post\/5b8959f96fb9a019fe685eb3\">https:\/\/juejin.im\/post\/5b8959f96fb9a019fe685eb3<\/a><\/p>\n<h3>OKHttp \u8bf7\u6c42\u7f51\u7edc CLEARTEXT communication to host not permitted by network<\/h3>\n<p>\u95ee\u9898\uff1a\u7531\u4e8e<code>Android P<\/code>\u9650\u5236\u4e86\u660e\u6587\u6d41\u91cf\u7684\u7f51\u7edc\u8bf7\u6c42\uff0c\u975e\u52a0\u5bc6\u7684\u6d41\u91cf\u8bf7\u6c42\u90fd\u4f1a\u88ab\u7cfb\u7edf\u7981\u6b62\u6389\u3002<\/p>\n<p>\u5982\u679c\u5f53\u524d\u5e94\u7528\u7684\u8bf7\u6c42\u662f<code>http<\/code>\u8bf7\u6c42\uff0c\u800c\u975e<code>https<\/code>\uff0c\u5c31\u4f1a\u5bfc\u7cfb\u7edf\u7981\u6b62\u5f53\u524d\u5e94\u7528\u8fdb\u884c\u8be5\u8bf7\u6c42\uff0c\u5982\u679c<code>WebView<\/code>\u7684url\u4f7f\u7528 <code>http<\/code>\u534f\u8bae\uff0c\u540c\u6837\u4f1a\u51fa\u73b0\u52a0\u8f7d\u5931\u8d25\uff0c<code>https<\/code>\u4e0d\u53d7\u5f71\u54cd\u3002<\/p>\n<p>\u4e3a\u6b64\uff0c<code>OkHttp3<\/code>\u505a\u4e86\u68c0\u67e5\uff0c\u5982\u679c\u4f7f\u7528\u4e86\u660e\u6587\u6d41\u91cf\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5728<code>Android P<\/code>\u7248\u672c<code>OkHttp3<\/code>\u5c31\u629b\u51fa\u5f02\u5e38:\u00a0<code>CLEARTEXT communication to &quot; + host + &quot; not permitted by network security policy<\/code><\/p>\n<pre><code class=\"language-java\">if (!Platform.get().isCleartextTrafficPermitted(host)) {\n    throw new RouteException(new UnknownServiceException(\n        &quot;CLEARTEXT communication to &quot; + host + &quot; not permitted by network security policy&quot;));\n}<\/code><\/pre>\n<p>\u89e3\u51b3\uff1a<\/p>\n<p>\uff081\uff09\u5728res\u4e0b\u65b0\u5efa\u4e00\u4e2axml\u76ee\u5f55\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\uff1a<code>network_security_config.xml<\/code>\u6587\u4ef6\uff0c\u8be5\u6587\u4ef6\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\n&lt;network-security-config&gt;\n    &lt;base-config cleartextTrafficPermitted=&quot;true&quot; \/&gt;\n&lt;\/network-security-config&gt;<\/code><\/pre>\n<p>\uff082\uff09\u7136\u540e\u5728<code>AndroidManifest.xml<\/code>\u7684<code>application<\/code>\u6807\u7b7e\u5185\u5e94\u7528\u4e0a\u9762\u7684xml\u914d\u7f6e\uff1a<\/p>\n<pre><code class=\"language-xml\">&lt;application\n    android:name=&quot;.AppApplication&quot;\n    android:icon=&quot;@mipmap\/ic_launcher&quot;\n    android:label=&quot;@string\/app_name&quot;\n    android:networkSecurityConfig=&quot;@xml\/network_security_config&quot;\n    android:roundIcon=&quot;@mipmap\/ic_launcher_round&quot;\n    android:theme=&quot;@style\/AppTheme&quot;&gt;\n&lt;\/application&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 org.apache.http.legacy \u5e93\u5728Android 9.0\u4e0a\u8fd0\u884c\u51fa\u73b0\u5d29\u6e83 \u65e5\u5fd7\u5185\u5bb9 jav [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61],"tags":[],"class_list":["post-1242","post","type-post","status-publish","format-standard","hentry","category-android-basic"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1242","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1242"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1242\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1242"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1242"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}