{"id":123,"date":"2018-09-30T00:39:35","date_gmt":"2018-09-29T15:39:35","guid":{"rendered":"https:\/\/www.dogrow.net\/android\/?p=123"},"modified":"2018-09-30T01:06:26","modified_gmt":"2018-09-29T16:06:26","slug":"blog7","status":"publish","type":"post","link":"https:\/\/www.dogrow.net\/android\/blog7\/","title":{"rendered":"(7) Android\u30a2\u30d7\u30ea\u304b\u3089 HTTP\u3067\u30b5\u30a4\u30c8\u306b\u63a5\u7d9a\u3059\u308b\u3002"},"content":{"rendered":"<h1 class=\"my_h\">1. \u3084\u308a\u305f\u3044\u3053\u3068<\/h1>\n<p><a href=\"https:\/\/www.dogrow.net\/android\/blog4\/\" target=\"_blank\">(4) BMI\u8a08\u7b97\u30a2\u30d7\u30ea\u3092\u4f5c\u308b\u3002<\/a> \u3067\u306f\u3001\u5165\u529b\u3055\u308c\u305f\u8eab\u9577\u3001\u4f53\u91cd\u304b\u3089 BMI\u5024\u3092\u7b97\u51fa\u3057\u3066\u8868\u793a\u3059\u308b\u30a2\u30d7\u30ea\u3092\u4f5c\u3063\u305f\u3002<\/p>\n<p>\u4eca\u56de\u306f\u7b97\u51fa\u3057\u305f BMI\u5024\u3092 WEB\u4e0a\u3067\u4fdd\u5b58\u3059\u3079\u304f\u3001HTTP\u30d7\u30ed\u30c8\u30b3\u30eb\u3067 WEB\u30b5\u30fc\u30d0\u30fc\u3068\u7e4b\u3052\u3066\u307f\u305f\u3044\u3002<br \/>\n<span class=\"my_fc_crimson\">\u203b\u305f\u307e\u305f\u307e\u81ea\u5206\u304c\u4f7f\u3063\u3066\u3044\u308b\u30b5\u30a4\u30c8\u304c HTTPS\u30aa\u30f3\u30ea\u30fc\u306b\u8a2d\u5b9a\u3057\u3066\u3044\u308b\u305f\u3081\u3001HTTPS\u3092\u4f7f\u7528\u3059\u308b\u3002<\/span><\/p>\n<p>\u3044\u308d\u3044\u308d\u3068\u6a5f\u80fd\u3066\u3093\u3053\u76db\u308a\u306b\u3059\u308b\u3068\u606f\u5207\u308c\u3057\u3066\u3057\u307e\u3046\u306e\u3067\u3001<br \/>\n\u307e\u305a\u306f GET\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u3067\u8eab\u9577\u3001\u4f53\u91cd\u3001BMI\u3092\u6e21\u3059\u3068\u3053\u308d\u307e\u3067\u3092\u4f5c\u3063\u3066\u307f\u308b\u3002<\/p>\n<h1 class=\"my_h\">2. \u3084\u3063\u3066\u307f\u308b\uff01<\/h1>\n<h2 class=\"my_h\">(1) \u65b9\u91dd<\/h2>\n<p><a href=\"https:\/\/www.dogrow.net\/android\/blog4\/\" target=\"_blank\">(4) BMI\u8a08\u7b97\u30a2\u30d7\u30ea\u3092\u4f5c\u308b\u3002<\/a> \u3067\u4f5c\u3063\u305f\u30d7\u30ed\u30b0\u30e9\u30e0\u304b\u3089\u306e\u5909\u66f4\u70b9\u306f\u308f\u305a\u304b\u3060\u3002<br \/>\n1) [\u8a08\u7b97\u5b9f\u884c]\u30dc\u30bf\u30f3\u62bc\u4e0b\u3067 BMI\u5024\u3092\u7b97\u51fa\u3057\u305f\u5f8c\u3001HTTP\u306e GET\u3067\u8eab\u9577\u3001\u4f53\u91cd\u3001BMI\u3092 WEB\u30b5\u30fc\u30d0\u30fc\u306b\u9001\u4fe1\u3059\u308b\u3002<br \/>\n2) WEB\u30b5\u30fc\u30d0\u30fc\u5074\u3067\u306f\u6e21\u3055\u308c\u305f\u5404\u5024\u3092\u30d5\u30a1\u30a4\u30eb\u4fdd\u5b58\u3059\u308b\u3002<br \/>\n\u4ee5\u4e0a\u3060\u3002<\/p>\n<h2 class=\"my_h\">(2) \u30de\u30cb\u30e5\u30d5\u30a7\u30b9\u30c8\u306e\u5909\u66f4<\/h2>\n<p>\u81ea\u52d5\u751f\u6210\u3055\u308c\u305f\u30de\u30cb\u30e5\u30d5\u30a7\u30b9\u30c8\u3067\u306f\u3001\u30a4\u30f3\u30bf\u30fc\u30cd\u30c3\u30c8\u63a5\u7d9a\u304c NG\u306b\u306a\u3063\u3066\u3044\u308b\u3002<br \/>\n\u30de\u30cb\u30e5\u30d5\u30a7\u30b9\u30c8\u30d5\u30a1\u30a4\u30eb\u3092\u66f8\u304d\u63db\u3048\u3066\u3053\u308c\u3092\u8a31\u53ef\u3059\u308b\u3002<\/p>\n<p><b>AndroidManufest.xml<\/b><br \/>\n<span class=\"my_fc_deeppink\">uses-permission<\/span> \u306e\u884c\u3092\u8ffd\u52a0\u3059\u308b\u3002<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;manifest xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;\r\n    package=&quot;net.dogrow.www.mybmi&quot;&gt;\r\n    &lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; \/&gt;\r\n<\/pre>\n<p>\u3067\u306a\u3044\u3068\u3001HttpsURLConnection::connect() \u3067\u3053\u3093\u306a\u4f8b\u5916\u304c\u767a\u751f\u3059\u308b\u3002<\/p>\n<pre>\r\njava.lang.SecurityException: Permission denied (missing INTERNET permission?)\r\n<\/pre>\n<h2 class=\"my_h\">(3) \u5b9f\u88c5<\/h2>\n<p><a href=\"https:\/\/www.dogrow.net\/android\/blog4\/\" target=\"_blank\">(4) BMI\u8a08\u7b97\u30a2\u30d7\u30ea\u3092\u4f5c\u308b\u3002<\/a> \u304b\u3089\u306e\u5909\u66f4\u7b87\u6240\u3060\u3051\u3092\u8a18\u3059\u3002<\/p>\n<p><b>MainActivity.java<\/b><br \/>\nonClick() \u306e\u6700\u5f8c\u306b\u2605\u2605\u2605\u3092\u8ffd\u52a0\u3059\u308b\u3002<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic void onClick(View v){\r\n  \/\/ \u8eab\u9577\u3068\u4f53\u91cd\u306e\u5165\u529b\u5024\u3092\u53d6\u5f97\r\n  double input_H_double = Double.valueOf(input_H.getText().toString());\r\n  double input_W_double = Double.valueOf(input_W.getText().toString());\r\n  double BMI = calcBMI(input_H_double, input_W_double);\r\n  \/\/ BMI\u7b97\u51fa\u7d50\u679c\u3092\u8868\u793a\r\n  String result = String.format(&quot;%.1f&quot;, BMI);\r\n  bmi_result.setText(result);\r\n  \/\/\u2605\u2605\u2605 WEB\u306b\u30c7\u30fc\u30bf\u9001\u4fe1\r\n  HttpReq http_req = new HttpReq();\r\n  http_req.SendDataToWEB(input_H_double, input_W_double, BMI);\r\n}\r\n<\/pre>\n<p><b>HttpReq.java<\/b><br \/>\n\u3053\u306e\u30d5\u30a1\u30a4\u30eb\u306f\u30af\u30e9\u30b9\u306e\u65b0\u898f\u4f5c\u6210\u3067\u4f5c\u3063\u305f\u3002<br \/>\n\u901a\u4fe1\u7528\u30b9\u30ec\u30c3\u30c9\u3092\u751f\u6210\u3057\u3001\u305d\u306e\u4e2d\u3067\u8eab\u9577\u3001\u4f53\u91cd\u3001BMI\u3092\u5f15\u6570\u3068\u3057\u3066 HTTP\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u9001\u4fe1\u3057\u3066\u3044\u308b\u3002<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage net.dogrow.www.mybmi;\r\n\r\nimport android.util.Log;\r\n\r\nimport javax.net.ssl.HttpsURLConnection;\r\nimport java.net.URL;\r\n\r\npublic class HttpReq {\r\n  public void SendDataToWEB(double h, double w, double bmi){\r\n    final String url = String.format(&quot;https:\/\/www.dogrow.net\/android\/sample\/0007\/index.php?w=%f&amp;h=%f&amp;bmi=%f&quot;, w, h, bmi);\r\n    new Thread(new Runnable() {\r\n      @Override\r\n      public void run() {\r\n        HttpsURLConnection con = null;\r\n        try {\r\n          URL jnUrl = new URL(url);\r\n          con = (HttpsURLConnection)jnUrl.openConnection();\r\n          con.connect();\r\n          int status = con.getResponseCode();\r\n          if (status != HttpsURLConnection.HTTP_OK) {\r\n            Log.d(&quot;HttpReq.Thread&quot;, &quot;Failed. status = &quot; + status);\r\n          }\r\n        } catch(Exception e) {\r\n          System.out.println(e);\r\n        } finally{\r\n          if(con != null) {\r\n            con.disconnect();\r\n          }\r\n        }\r\n      }\r\n    }).start();\r\n  }\r\n}\r\n<\/pre>\n<p><b>index.php<\/b><br \/>\nWEB\u30b5\u30fc\u30d0\u30fc\u5074\u306b\u306f\u4ee5\u4e0b\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u7f6e\u3044\u305f\u3002<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?\r\n  if(count($_GET) &gt; 0){\r\n    $dt = strftime(&quot;%Y-%m-%d %H:%M:%S&quot;, time());\r\n    $add = '';\r\n\r\n    $fh = fopen(&quot;testG.txt&quot;, &quot;a+&quot;);\r\n    foreach($_GET as $k =&gt; $v){\r\n      $add .= &quot;{$k}:{$v} &quot;;\r\n    }\r\n    $line = &quot;{$dt} {$add}\\n&quot;;\r\n    fwrite($fh, $line);\r\n    fclose($fh);\r\n  }\r\n<\/pre>\n<h2 class=\"my_h\">(4) \u5b9f\u884c\u7d50\u679c<\/h2>\n<p>BMI\u8a08\u7b97\u306e\u5f8c\u3001WEB\u30b5\u30fc\u30d0\u30fc\u306b\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30d5\u30a1\u30a4\u30eb\u304c\u51fa\u6765\u3066\u3044\u305f\u3002 \u2192 OK!<\/p>\n<pre>\r\n2018-09-30 00:09:56 w:80.000000 h:178.000000 bmi:25.249337 \r\n2018-09-30 00:31:40 w:68.000000 h:174.000000 bmi:22.460034 \r\n<\/pre>\n<h1 class=\"my_h\">3. \u6240\u611f<\/h1>\n<p>\u30fbHTTP\u30ec\u30b9\u30dd\u30f3\u30b9\u306e\u53d6\u5f97\u306f\u5225\u306e\u6a5f\u4f1a\u306b\u3084\u3063\u3066\u307f\u308b\u3002<br \/>\n\u30fbHTTPS\u3067\u306f URL\u3082\u6697\u53f7\u5316\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u3001GET\u3067\u6e21\u3057\u305f BMI\u5024\u304c\u4f1d\u9001\u8def\u4e0a\u3067\u6f0f\u6d29\u3059\u308b\u53ef\u80fd\u6027\u306f\u4f4e\u3044\u3002<br \/>\n\u30fb\u30b5\u30fc\u30d0\u30fc\u5074\u3067\u306f BMI\u5024\u3092 DB\u306b\u4fdd\u5b58\u3059\u308b\u3088\u3046\u306b\u6539\u9020\u3057\u3088\u3046\u3002\uff08\u3044\u3064\u304b\uff09<br \/>\n\u30fb\u30b5\u30fc\u30d0\u30fc\u5074\u304b\u3089\u30b0\u30e9\u30d5\u753b\u50cf\u3092\u53d6\u5f97\u3057\u3066\u3082\u9762\u767d\u3044\u304b\u3082\u3002\uff08\u6570\u5024\u3092\u53d6\u5f97\u3057\u3066\u30b9\u30de\u30db\u5074\u3067\u30b0\u30e9\u30d5\u5316\u3067\u3082\u3088\u3044\uff09<\/p>\n<h1 class=\"my_h\">4. \u53c2\u8003<\/h1>\n<p>\u3042\u308a\u304c\u3068\u3046\u3054\u3056\u3044\u307e\u3059\u3002 m(_ _)m<br \/>\n<a href=\"https:\/\/hogem.hatenablog.com\/entry\/20100307\/1267977441\" target=\"_blank\">https:\/\/hogem.hatenablog.com\/entry\/20100307\/1267977441<\/a><\/p>\n<hr class=\"my_hr_bottom\">\n","protected":false},"excerpt":{"rendered":"<p>1. \u3084\u308a\u305f\u3044\u3053\u3068 (4) BMI\u8a08\u7b97\u30a2\u30d7\u30ea\u3092\u4f5c\u308b\u3002 \u3067\u306f\u3001\u5165\u529b\u3055\u308c\u305f\u8eab\u9577\u3001\u4f53\u91cd\u304b\u3089 BMI\u5024\u3092\u7b97\u51fa\u3057\u3066\u8868\u793a\u3059\u308b\u30a2\u30d7\u30ea\u3092\u4f5c\u3063\u305f\u3002 \u4eca\u56de\u306f\u7b97\u51fa\u3057\u305f BMI\u5024\u3092 WEB\u4e0a\u3067\u4fdd\u5b58\u3059\u3079\u304f\u3001HTTP\u30d7\u30ed\u30c8\u30b3\u30eb\u3067 WEB\u30b5\u30fc\u30d0\u30fc\u3068\u7e4b\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.dogrow.net\/android\/blog7\/\">\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-123","post","type-post","status-publish","format-standard","hentry","category-androidstudio","category-http"],"views":2211,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/posts\/123","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=123"}],"version-history":[{"count":11,"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/posts\/123\/revisions"}],"predecessor-version":[{"id":134,"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/posts\/123\/revisions\/134"}],"wp:attachment":[{"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/media?parent=123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/categories?post=123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/tags?post=123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}