{"id":22593,"date":"2024-04-28T15:08:15","date_gmt":"2024-04-28T07:08:15","guid":{"rendered":"https:\/\/www.yimenyun.cn\/emen\/?p=22593"},"modified":"2024-04-28T15:08:15","modified_gmt":"2024-04-28T07:08:15","slug":"android%e6%89%93%e5%8c%85%e7%9a%84apk%e4%b8%8d%e8%83%bd%e7%bd%91%e7%bb%9c%e8%af%b7%e6%b1%82%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/www.yimenyun.cn\/emen\/22593\/","title":{"rendered":"android\u6253\u5305\u7684apk\u4e0d\u80fd\u7f51\u7edc\u8bf7\u6c42\u95ee\u9898"},"content":{"rendered":"

\u5728Android\u5f00\u53d1\u4e2d\uff0c\u6253\u5305\u6210APK\u7684\u5e94\u7528\u7a0b\u5e8f\u662f\u53ef\u4ee5\u8fdb\u884c\u7f51\u7edc\u8bf7\u6c42\u7684\u3002\u7f51\u7edc\u8bf7\u6c42\u662f\u6307\u901a\u8fc7\u5e94\u7528\u7a0b\u5e8f\u5411\u8fdc\u7a0b\u670d\u52a1\u5668\u53d1\u9001\u8bf7\u6c42\uff0c\u5e76\u83b7\u53d6\u54cd\u5e94\u6570\u636e\u7684\u8fc7\u7a0b\u3002\u5728Android\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u5e93\u6216\u6846\u67b6\u6765\u5b9e\u73b0\u7f51\u7edc\u8bf7\u6c42\uff0c\u6700\u5e38\u7528\u7684\u6709\u539f\u751f\u7684HttpURLCon\u624b\u673agif\u5236\u4f5capk<\/a>nection\u548cOkHttp\u3002<\/p>\n

1. HttpURLConnection<\/p>\n

HttpURLConnection\u662fAndroid\u4e2d\u7684\u4e00\u4e2a\u539f\u751f\u7c7b\uff0c\u7528\u4e8e\u8fdb\u884c\u57fa\u672c\u7684\u7f51\u7edc\u8bf7\u6c42\u3002\u4f7f\u7528HttpURLConnection\u9700\u8981\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a<\/p>\n

\u9996\u5148\uff0c\u9700\u8981\u5728AndroidManifest.xml\u6587\u4ef6\u4e2d\u6dfb\u52a0\u7f51\u7edc\u8bbf\u95ee\u6743\u9650\uff1a<\/p>\n

“`xml<\/p>\n<\/p>\n

“`<\/p>\n

\u63a5\u4e0b\u6765\uff0c\u5728\u4f60\u7684Java\u7c7b\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u8fdb\u884c\u7f51\u7edc\u8bf7\u6c42\uff1a<\/p>\n

“`java<\/p>\n

try {<\/p>\n

\/\/ \u521b\u5efaURL\u5bf9\u8c61<\/p>\n

URL url = new URL(“http:\/\/www.example.com”);<\/p>\n

\/\/ \u6253\u5f00\u8fde\u63a5<\/p>\n

HttpURLConnection connection = (HttpURLConnection) url.openConnection();<\/p>\n

\/\/ \u8bbe\u7f6e\u8bf7\u6c42\u65b9\u6cd5<\/p>\n

connection.setRequestMethod(“GET”);<\/p>\n

\/\/ \u83b7\u53d6\u54cd\u5e94\u7801<\/p>\n

int responseCode = connection.getResponseCode();<\/p>\n

\/\/ \u5224\u65ad\u54cd\u5e94\u7801<\/p>\n

if (responseCode == HttpURLConnection.HTTP_OK) {<\/p>\n

\/\/ \u83b7\u53d6\u54cd\u5e94\u6570\u636e<\/p>\n

InputStream inputStream = connection.getInputStream();<\/p>\n

\/\/ \u5904\u7406\u54cd\u5e94\u6570\u636e<\/p>\n

\/\/ …<\/p>\n

}<\/p>\n

\/\/ \u5173\u95ed\u8fde\u63a5<\/p>\n

connection.disconnect();<\/p>\n

} catch (IOException e) {<\/p>\n

e.printStackTrace();<\/p>\n

}<\/p>\n

“`<\/p>\n

2. OkHttp<\/p>\n

OkHttp\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u7f51\u7edc\u8bf7\u6c42\u5e93\uff0c\u5177\u6709\u66f4\u65b9\u4fbf\u7684API\u548c\u66f4\u9ad8\u6548\u7684\u6027\u80fd\u3002\u4f7f\u7528OkHttp\u53ef\u4ee5\u51cf\u5c11\u5f88\u591a\u5197\u4f59\u7684\u4ee3\u7801\uff0c\u5e76\u4e14\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u529f\u80fd\u3002\u4f7f\u7528OkHttp\u9700\u8981\u5148\u5728\u4f60\u7684build.gradle\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4f9d\u8d56\uff1a<\/p>\n

“`groovy<\/p>\n

implementation ‘com.squareup.okhttp3:okhttp:4.9.1’<\/p>\n

“`<\/p>\n

\u63a5\u4e0b\u6765\uff0c\u5728\u4f60\u7684Java\u7c7b\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u8fdb\u884c\u7f51\u7edc\u8bf7\u6c42\uff1a<\/p>\n

“`java<\/p>\n

\/\/ \u521b\u5efaOkHttpClient\u5bf9\u8c61<\/p>\n

OkHttpClient client = new OkHttpClient();<\/p>\n

\/\/ \u521b\u5efaRequest\u5bf9\u8c61<\/p>\n

Re<\/p>\n

<\/figure>\n<\/p>\n

quest request = new Request.Builder(\u624b\u673aapk\u751f\u6210\u5668<\/a>)<\/p>\n

.url(“http:\/\/www.example.com”)<\/p>\n

.build();<\/p>\n

\/\/ \u53d1\u9001\u8bf7\u6c42<\/p>\n

try {<\/p>\n

Response response = client.newCall(request).execute();<\/p>\n

\/\/ \u83b7\u53d6\u54cd\u5e94\u6570\u636e<\/p>\n

if (response.isSuccessful()) {<\/p>\n

\/\/ \u5904\u7406\u54cd\u5e94\u6570\u636e<\/p>\n

\/\/ …<\/p>\n

}<\/p>\n

} catch (IOException e) {<\/p>\n

e.printStackTrace();<\/p>\n

}<\/p>\n

“`<\/p>\n

\u4ee5\u4e0a\u662f\u4e24\u79cd\u5e38\u7528\u7684\u8fdb\u884c\u7f51\u7edc\u8bf7\u6c42\u7684\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u9009\u62e9\u5176\u4e2d\u4e00\u79cd\u3002\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728\u8fdb\u884c\u7f51\u7edc\u8bf7\u6c42\u65f6\uff0c\u9700\u8981\u5728AndroidManifest.xml\u6587\u4ef6\u4e2d\u58f0\u660e\u7f51\u7edc\u8bbf\u95ee\u6743\u9650\uff0c\u5426\u5219\u4f1a\u629b\u51faSecurityException\u3002<\/p>\n

\u53e6\u5916\uff0c\u4e3a\u4e86\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u548c\u5b89\u5168\u6027\uff0c\u5efa\u8bae\u5c06\u7f51\u7edc\u8bf7\u6c42\u653e\u5728\u5f02\u6b65\u7ebf\u7a0b\u4e2d\u6267\u884c\uff0c\u4ee5\u514d\u963b\u585e\u4e3b\u7ebf\u7a0b\u6216\u9020\u6210ANR\uff08Application Not Responding\uff09\u3002<\/p>\n

\u7efc\u4e0a\u6240\u8ff0\uff0cAndroid\u5e94\u7528\u7a0b\u5e8f\u6253\u5305\u6210APK\u540e\uff0c\u4ecd\u7136\u53ef\u4ee5\u8fdb\u884c\u7f51\u7edc\u8bf7\u6c42\u3002\u901a\u8fc7HttpURLConnection\u6216OkHttp\u7b49\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u7b80\u6d01\u9ad8\u6548\u7684\u7f51\u7edc\u8bf7\u6c42\u529f\u80fd\uff0c\u4e3a\u5e94\u7528\u7a0b\u5e8f\u63d0\u4f9b\u4e30\u5bcc\u7684\u6570\u636e\u4ea4\u4e92\u80fd\u529b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5728Android\u5f00\u53d1\u4e2d\uff0c\u6253\u5305\u6210APK\u7684\u5e94\u7528\u7a0b\u5e8f\u662f\u53ef\u4ee5\u8fdb\u884c\u7f51\u7edc\u8bf7\u6c42\u7684\u3002\u7f51\u7edc\u8bf7\u6c42\u662f\u6307\u901a\u8fc7\u5e94\u7528\u7a0b\u5e8f\u5411\u8fdc\u7a0b\u670d\u52a1\u5668\u53d1\u9001\u8bf7\u6c42\uff0c\u5e76\u83b7\u53d6\u54cd\u5e94\u6570\u636e\u7684\u8fc7\u7a0b\u3002\u5728Android\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u5e93\u6216\u6846\u67b6\u6765\u5b9e\u73b0\u7f51\u7edc\u8bf7\u6c42\uff0c\u6700<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[23880,12,168,23879,2733],"topic":[],"class_list":["post-22593","post","type-post","status-publish","format-standard","hentry","category-appkf","tag-autojsapk","tag-12","tag-168","tag-64apk","tag-2733"],"_links":{"self":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts\/22593","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/comments?post=22593"}],"version-history":[{"count":1,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts\/22593\/revisions"}],"predecessor-version":[{"id":22621,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts\/22593\/revisions\/22621"}],"wp:attachment":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/media?parent=22593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/categories?post=22593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/tags?post=22593"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/topic?post=22593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}