{"id":568,"date":"2023-01-13T10:17:29","date_gmt":"2023-01-13T01:17:29","guid":{"rendered":"https:\/\/www.dogrow.net\/android\/?p=568"},"modified":"2023-01-14T01:47:39","modified_gmt":"2023-01-13T16:47:39","slug":"blog30","status":"publish","type":"post","link":"https:\/\/www.dogrow.net\/android\/blog30\/","title":{"rendered":"(30) AndroidEmulator\u304b\u3089HTTP\u63a5\u7d9a\u3057\u3066\u753b\u50cf\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3059\u308b\u3002"},"content":{"rendered":"<h1 class=\"my_h\">1. \u3084\u308a\u305f\u3044\u3053\u3068<\/h1>\n<p>AndroidStudio\u3067\u30a2\u30d7\u30ea\u3092\u4f5c\u308a\u3001\u305d\u306e\u30a2\u30d7\u30ea\u4e0a\u3067\u3001\u30a4\u30f3\u30bf\u30fc\u30cd\u30c3\u30c8\u304b\u3089\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u305f\u753b\u50cf\u3092\u8868\u793a\u3057\u305f\u3044\u3002<\/p>\n<h1 class=\"my_h\">2. \u3084\u3063\u3066\u307f\u305f<\/h1>\n<p>\u524d\u6295\u7a3f\u3067\u306f\u3044\u308d\u3044\u308d\u3068\u624b\u9806\u3092\u7aef\u6298\u3063\u305f\u305f\u3081\u306b\u3001\u5f8c\u3067\u898b\u76f4\u3057\u305f\u3068\u304d\u306b\uff1f\uff1f\uff1f\u306b\u306a\u3063\u305f\u3002<br \/>\n\u4eca\u56de\u306f\u3001\u4e01\u5be7\u306b\u624b\u9806\u3092\u8a18\u3057\u3066\u884c\u3053\u3046\u3068\u601d\u3046\u3002<\/p>\n<h2 class=\"my_h\">(1) AndroidStudio\u4e0a\u3067\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u65b0\u898f\u4f5c\u6210\u3059\u308b\u3002<\/h2>\n<p>1) [New Project] \u30e1\u30cb\u30e5\u30fc\u3092\u9078\u629e\u3059\u308b\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/android\/wp-content\/uploads\/2023\/01\/i001.jpg\" alt=\"\" class=\"my_add_bs1\" \/><\/p>\n<p>2) [Empty Activity]\u3092\u9078\u629e\u3059\u308b\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/android\/wp-content\/uploads\/2023\/01\/i002.jpg\" alt=\"\" class=\"my_add_bs1\" \/><\/p>\n<p>3) \u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u540d\u3092\u6c7a\u5b9a\u3057\u3066 [Finish] \u3059\u308b\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/android\/wp-content\/uploads\/2023\/01\/i003.jpg\" alt=\"\" class=\"my_add_bs1\" \/><\/p>\n<h2 class=\"my_h\">(2) \u30a2\u30d7\u30ea\u3067 HTTPS\u901a\u4fe1\u51fa\u6765\u308b\u3088\u3046\u306b\u8a2d\u5b9a\u3059\u308b\u3002<\/h2>\n<h3 class=\"my_h\">(1) INTERNET\u30a2\u30af\u30bb\u30b9\u3092\u8a31\u53ef\u3059\u308b\u3002<\/h3>\n<p><span class=\"my_fc_blueB\">AndroidManufest.xml<\/span> \u306b 4\u884c\u76ee\u3092\u8ffd\u8a18\u3059\u308b\u3002<\/p>\n<pre class=\"brush: xml; highlight: [4]; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\r\n&lt;manifest xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;\r\n    xmlns:tools=&quot;http:\/\/schemas.android.com\/tools&quot;&gt;\r\n    &lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; \/&gt;\r\n    &lt;application\r\n        android:allowBackup=&quot;true&quot;\r\n        android:dataExtractionRules=&quot;@xml\/data_extraction_rules&quot;\r\n        android:fullBackupContent=&quot;@xml\/backup_rules&quot;\r\n<\/pre>\n<h3 class=\"my_h\">(2) HTTPS\u63a5\u7d9a\u3092\u5f37\u5236\u3059\u308b\u3002\uff08\u5fc5\u8981\u306a\u5834\u5408\uff09<\/h3>\n<p>\u65b0\u898f\u306b XML\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3059\u308b\u3002\u540d\u524d\u306f\u4f55\u3067\u3082\u3088\u3044\u3002<br \/>\n\u3053\u3053\u3067\u306f <span class=\"my_fc_blueB\">communication_rules.xml<\/span> \u3068\u3057\u3066\u4f5c\u6210\u3057\u3001<span class=\"my_fc_blueB\">\/res\/xml<\/span> \u30d5\u30a9\u30eb\u30c0\u306b\u914d\u7f6e\u3057\u305f\u3002<\/p>\n<p><span class=\"my_fw_bold my_fs_big2\">\u624b\u9806\uff1a<\/span><br \/>\n1. xml\u30d5\u30a9\u30eb\u30c0\u3092\u9078\u629e\u3059\u308b\u3002<br \/>\n2. \u53f3\u30af\u30ea\u30c3\u30af\u3057\u3066 popup menu\u3092\u8868\u793a\u3059\u308b\u3002<br \/>\n3. [New]\uff0d[File]\u30e1\u30cb\u30e5\u30fc\u3092\u9078\u629e\u3059\u308b\u3002<br \/>\n4. \u30d5\u30a1\u30a4\u30eb\u540d <span class=\"my_fc_blueB\">communication_rules.xml<\/span> \u3092\u5165\u529b\u3059\u308b\u3002<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;network-security-config&gt;\r\n    &lt;domain-config usesCleartextTraffic=&quot;false&quot;&gt;\r\n        &lt;domain includeSubdomains=&quot;true&quot;&gt;www.dogrow.net&lt;\/domain&gt;\r\n    &lt;\/domain-config&gt;\r\n&lt;\/network-security-config&gt;\r\n<\/pre>\n<p>\u6b21\u306b\u3001\u4f5c\u3063\u305f\u30d5\u30a1\u30a4\u30eb\u3092 <span class=\"my_fc_blueB\">AndroidManufest.xml<\/span> \u3067\u30a4\u30f3\u30dd\u30fc\u30c8\u3059\u308b\u30027\u884c\u76ee\u3092\u8ffd\u8a18\u3059\u308b\u3002<\/p>\n<pre class=\"brush: xml; highlight: [7]; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\r\n&lt;manifest xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;\r\n\u00a0\u00a0\u00a0\u00a0xmlns:tools=&quot;http:\/\/schemas.android.com\/tools&quot;&gt;\r\n\u00a0\u00a0\u00a0\u00a0&lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; \/&gt;\r\n\u00a0\u00a0\u00a0\u00a0&lt;!-- android:usesCleartextTraffic=&quot;true&quot; --&gt;\r\n\u00a0\u00a0\u00a0\u00a0&lt;application\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0android:networkSecurityConfig=&quot;@xml\/communication_rules&quot;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0android:allowBackup=&quot;true&quot;\r\n :\r\n<\/pre>\n<h3 class=\"my_h\">(3) Gradle\u306b OkHTTP\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u8ffd\u52a0<\/h3>\n<p><a href=\"https:\/\/gradle.org\/\" target=\"_blank\" rel=\"noopener\">Gradle<\/a>\u306f\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u66f8\u3044\u3066\u30d3\u30eb\u30c9\u5b9f\u884c\u3092\u5236\u5fa1\u3059\u308b\u30c4\u30fc\u30eb\u3002<br \/>\n\u4eca\u56de\u306f <a href=\"https:\/\/square.github.io\/okhttp\/\" target=\"_blank\" rel=\"noopener\">OkHttp<\/a>\u3092\u30a4\u30f3\u30dd\u30fc\u30c8\u3057\u3066\u4f7f\u3046\u3002<\/p>\n<p><span class=\"my_fc_blueB\">build.gradle.<\/span><br \/>\n7\u884c\u76ee\u3092\u8ffd\u8a18\u3059\u308b\u3002<\/p>\n<pre class=\"brush: bash; highlight: [8]; title: ; notranslate\" title=\"\">\r\ndependencies {\r\n    implementation 'androidx.appcompat:appcompat:1.4.1'\r\n    implementation 'com.google.android.material:material:1.5.0'\r\n    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'\r\n    testImplementation 'junit:junit:4.13.2'\r\n    androidTestImplementation 'androidx.test.ext:junit:1.1.3'\r\n    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'\r\n    implementation 'com.squareup.okhttp3:okhttp:4.10.0'\r\n}\r\n<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/android\/wp-content\/uploads\/2022\/11\/i001-4.jpg\" alt=\"\" class=\"my_add_bs1\" \/><\/p>\n<p>\u8a18\u8ff0\u5f8c\u306f\u3001Sync now \u30e1\u30cb\u30e5\u30fc\u3092\u9078\u629e\u3057\u3001\u30d1\u30c3\u30b1\u30fc\u30b8\u306e\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3092\u5b9f\u884c\u3059\u308b\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/android\/wp-content\/uploads\/2022\/11\/i002-4.jpg\" alt=\"\" class=\"my_add_bs1\" \/><\/p>\n<h2 class=\"my_h\">(3) \u753b\u50cf\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3059\u308b\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u66f8\u304f\u3002<\/h2>\n<p>\u4ee5\u4e0b\u306e\u6a5f\u80fd\u3092\u6301\u3064 Java\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u66f8\u3044\u305f\u3002<br \/>\n1) \u30dc\u30bf\u30f3\u62bc\u4e0b\u306e\u30a4\u30d9\u30f3\u30c8\u30cf\u30f3\u30c9\u30e9 onClick\u306b MainActivity.updateImg() \u3092\u767b\u9332\u3057\u305f\u3002<br \/>\n2) updateImg()\u3067\u306f\u3001HTTP\u30ea\u30af\u30a8\u30b9\u30c8\u3067\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u306e URL\u3092\u6307\u5b9a\u3057\u3066\u753b\u50cf\u53d6\u5f97\u3092\u8981\u6c42\u3059\u308b\u3002<br \/>\n3) HTTP\u30ec\u30b9\u30dd\u30f3\u30b9\u306e\u53d7\u4fe1\u30cf\u30f3\u30c9\u30e9 onResponse \u3067\u306f\u3001\u753b\u50cf\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u3002<br \/>\n\u3000\u2192 \u30e1\u30a4\u30f3\u30b9\u30ec\u30c3\u30c9\u5074\u3067\u753b\u50cf\u3092 View\u306b\u8868\u793a\u3059\u308b\u3002<\/p>\n<p><span class=\"my_fc_crimsonBBig\">Android\u30a2\u30d7\u30ea\u3067\u306f\u3001\u30e1\u30a4\u30f3\u30b9\u30ec\u30c3\u30c9\u4ee5\u5916\u304b\u3089 UI\u306b\u95a2\u3059\u308b\u64cd\u4f5c(\u4eca\u56de\u306e\u5834\u5408\u306f\u753b\u50cf\u8868\u793a)\u304c\u3067\u304d\u306a\u3044\u3002<\/span><br \/>\n<span class=\"my_fc_crimson\">UI\u5236\u5fa1\u3092\u62c5\u5f53\u3059\u308b\u30e1\u30a4\u30f3\u30b9\u30ec\u30c3\u30c9\u3067\u306f\u3001\u5185\u90e8\u7684\u306b Looper\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u6301\u3063\u3066\u3044\u308b\u3002<br \/>\nLooper\u306f MessageQueue\u3092\u6301\u3061\u3001\u5916\u90e8\u304b\u3089\u306e\u51e6\u7406\u4f9d\u983c\u3092\u53d7\u3051\u4ed8\u3051\u308b\u3002<br \/>\nMessageQueue\u306f\u3001Message, Runnable\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u53d7\u3051\u53d6\u308a\u3001\u305d\u308c\u3092\u51e6\u7406\u3059\u308b\u3002<\/span><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class MainActivity extends AppCompatActivity {\r\n    private Handler handler;\r\n\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n        handler = new Handler(Looper.getMainLooper());\r\n    }\r\n\r\n    public void updateImg(View view){\r\n        String url = &quot;https:\/\/www.dogrow.net\/nbdx\/ImgDL\/fruit_ao_ringo.png&quot;;\r\n        OkHttpClient httpClient = new OkHttpClient();\r\n        Request request = new Request.Builder().url(url).build();\r\n\r\n        httpClient.newCall(request).enqueue(new Callback(){\r\n            @Override\r\n            public void onResponse(@NotNull Call call, @NotNull Response resp) throws IOException {\r\n                InputStream is = resp.body().byteStream();\r\n                Bitmap bmp = BitmapFactory.decodeStream(is);\r\n                handler.post(() -&gt; {\r\n                    ImageView iv = findViewById(R.id.imgDLView);\r\n                    iv.setImageBitmap(bmp);\r\n                });\r\n            }\r\n            @Override\r\n            public void onFailure(@NotNull Call call, @NotNull IOException e){\r\n                Log.e(&quot;Step2&quot;, e.getMessage());\r\n            }\r\n        });\r\n    }\r\n}\r\n<\/pre>\n<p><span class=\"my_fs_big2 my_fw_bold\">\u53c2\u8003 :<\/span><br \/>\nOkHttp\u306e Callback Class\u4ed5\u69d8\u306f\u3053\u3061\u3089\u3092\u53c2\u7167\u306e\u3053\u3068\u3002<br \/>\n<a href=\"https:\/\/square.github.io\/okhttp\/4.x\/okhttp\/okhttp3\/-callback\/\" target=\"_blank\" rel=\"noopener\">OkHttp Callback<\/a><\/p>\n<p>\u611a\u76f4\u306b Callback\u30af\u30e9\u30b9\u306e\u30b5\u30d6\u30af\u30e9\u30b9\u3092\u4f5c\u308a\u3001\u3053\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u6e21\u3057\u3066\u3082\u3088\u3044\u3002<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class MainActivity extends AppCompatActivity {\r\n    private Handler handler;\r\n\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n        handler = new Handler(Looper.getMainLooper());\r\n    }\r\n\r\n    public void updateImg(View view){\r\n        String url = &quot;https:\/\/www.dogrow.net\/nbdx\/ImgDL\/fruit_ao_ringo.png&quot;;\r\n        OkHttpClient httpClient = new OkHttpClient();\r\n        Request request = new Request.Builder().url(url).build();\r\n\r\n        class MyCallback implements Callback{\r\n            @Override\r\n            public void onResponse(@NotNull Call call, @NotNull Response resp) throws IOException {\r\n                InputStream is = resp.body().byteStream();\r\n                Bitmap bmp = BitmapFactory.decodeStream(is);\r\n                handler.post(() -&gt; {\r\n                    ImageView iv = findViewById(R.id.imgDLView);\r\n                    iv.setImageBitmap(bmp);\r\n                });\r\n            }\r\n            @Override\r\n            public void onFailure(@NotNull Call call, @NotNull IOException e){\r\n                Log.e(&quot;Step2&quot;, e.getMessage());\r\n            }\r\n        }\r\n        MyCallback myCallback = new MyCallback();\r\n        httpClient.newCall(request).enqueue(myCallback);\r\n    }\r\n}\r\n<\/pre>\n<h2 class=\"my_h\">(4) UI\u30b9\u30ec\u30c3\u30c9\u3078\u306e\u51e6\u7406\u4f9d\u983c\u306e\u65b9\u6cd5\u3044\u308d\u3044\u308d<\/h2>\n<h3 class=\"my_h\">(1) \u611a\u76f4\u306b\u66f8\u304f\u3002<\/h3>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nclass myRunner implements Runnable {\r\n    @Override\r\n    public void run() {\r\n        ImageView iv = findViewById(R.id.imgDLView);\r\n        iv.setImageBitmap(bmp);\r\n    }\r\n}\r\nRunnable runner = new myRunner();\r\nhandler.post(runner);\r\n<\/pre>\n<h3 class=\"my_h\">(2) \u7121\u540d\u30af\u30e9\u30b9\u3067\u66f8\u304f\u3002<\/h3>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nRunnable runner = new Runnable() {\r\n    @Override\r\n    public void run() {\r\n        ImageView iv = findViewById(R.id.imgDLView);\r\n        iv.setImageBitmap(bmp);\r\n    }\r\n};\r\nhandler.post(runner);\r\n<\/pre>\n<h3 class=\"my_h\">(3) \u4e0a\u8a18(2)\u3092\u3055\u3089\u306b\u77ed\u7e2e\u3057\u3066\u66f8\u304f\u3002<\/h3>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nhandler.post(new Runnable() {\r\n    @Override\r\n    public void run() {\r\n        ImageView iv = findViewById(R.id.imgDLView);\r\n        iv.setImageBitmap(bmp);\r\n    }\r\n});\r\n<\/pre>\n<h3 class=\"my_h\">(4) \u30e9\u30e0\u30c0\u5f0f\u3067\u66f8\u304f\u3002<\/h3>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nhandler.post(() -&gt; {\r\n    ImageView iv = findViewById(R.id.imgDLView);\r\n    iv.setImageBitmap(bmp);\r\n});\r\n<\/pre>\n<hr class=\"my_hr_bottom\">\n","protected":false},"excerpt":{"rendered":"<p>1. \u3084\u308a\u305f\u3044\u3053\u3068 AndroidStudio\u3067\u30a2\u30d7\u30ea\u3092\u4f5c\u308a\u3001\u305d\u306e\u30a2\u30d7\u30ea\u4e0a\u3067\u3001\u30a4\u30f3\u30bf\u30fc\u30cd\u30c3\u30c8\u304b\u3089\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u305f\u753b\u50cf\u3092\u8868\u793a\u3057\u305f\u3044\u3002 2. \u3084\u3063\u3066\u307f\u305f \u524d\u6295\u7a3f\u3067\u306f\u3044\u308d\u3044\u308d\u3068\u624b\u9806\u3092\u7aef\u6298\u3063\u305f\u305f\u3081\u306b\u3001\u5f8c\u3067\u898b\u76f4\u3057\u305f\u3068\u304d\u306b\uff1f\uff1f\uff1f\u306b\u306a\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.dogrow.net\/android\/blog30\/\">\u7d9a\u304d\u3092\u8aad\u3080 &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,3],"tags":[],"class_list":["post-568","post","type-post","status-publish","format-standard","hentry","category-androidstudio","category-http"],"views":879,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/posts\/568","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/comments?post=568"}],"version-history":[{"count":38,"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/posts\/568\/revisions"}],"predecessor-version":[{"id":619,"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/posts\/568\/revisions\/619"}],"wp:attachment":[{"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/media?parent=568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/categories?post=568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/tags?post=568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}