4.11 HttpURLConnection connection. setRequestMethod("GET") ; connection. setRequestProperty ("Host ” connection. connect ( ) ; connection. getResponseCode() ; int responseCode Log. d("TEST" responseCode= ”十 responseC0de) ; String contentLength = connection. getHeaderFie1d("Content-Length") ; Log. d("TEST ”” Content -Length=" + contentLength) ; String contentType = connection. getHeaderFie1d("Content-Type't) ; Log. d("TEST" ” contentType=" 十 contentType) ; lnputStream inputStream = connection. getInputStream() ; String body = readTOEnd (inputStream) ; Log. d("TEST" , "body=" + body) ; inputStream. close ( ) ; } catch (Ma1formedURLException e) { throw new RuntimeException(e) ; } catch (IOException e) { throw new RuntimeException(e) ; "tomorrovkey. github. iO ” ) ; HttpURLConnection では、 どのような処理をしているか順番に見ていきま 4.11.1 リクエストを送信する アクセスする URL を使い URL オプジェクトを作ります ( リスト 4.18 ) 。 リスト 4.18 : URL オプジェクトの生成 URL url = new URL("http://tomorrowkey.github.io ” ) ; openConnection() メソッドを使い、 HttpURLConnection を取得します ( リ スト 4.19 ) 。 リスト 4.19 : HTTPUrlConnection の取得 HttpURLConnection connection = (HttpURLConnection) url. openConnection() ; 注意点として、 URL. openConnection() メソッドの戻り値は URLConnection 93
第 4 章ネットワークプログラミング D/TEST D/TEST D/TEST D/TEST D/TEST D/TEST D/TEST D/TEST D/TEST D/TEST D/TEST D/TEST D/TEST D/TEST D/TEST D/TEST D/TEST D/TEST D/TEST D/TEST D/TEST D/TEST D/TEST D/TEST D/TEST D/TEST 1371 ) : 1371 ) : 1371 ) : ( 1371 ) : 1371 ) : 1371 ) : 1371 ) : 1371 ) : 1371 ) : 1371 ) : 1371 ) : 1371 ) : 1371 ) : 1371 ) : 1371 ) : 1371 ) : 1371 ) : 1371 ) : 1371 ) : 1371 ) : 1371 ) : 1371 ) : 1371 ) : 1371 ) : 1371 ) : く /html> く /body> く hl > He110 , tomorrow! ! く / hl > く body> く /head> く meta charset="UTF-8" / > く title>tomorrowkey GitHub page く /title> く head> く html lang="j a"> く ! DOCTYPE html> く html> Vary : Accept-Encoding 1371 ) : X-Timer: S1404131042.773465872 , VSO , VE174 X-Cache-Hits: 0 X-Cache : MISS X-Served-By: cache-ty66-TYO Connection: keep-alive Age : 3409 Via: 1 . 1 varnish Date: Mon, 30 Jun 2014 12 : 24 : 02 GMT Accept-Ranges : bytes Content-Length: 169 Cache-Contr01 : max-age=600 Expires: Mon, 30 Jun 2014 11 : 37 : 13 GMT Last-Modified: Mon, 02 Jan 2012 05 : 54 : 49 GMT HTTP レスポンスの header と body 両方が出力されました。 Socket で通信す る場合はこの文字列を Java のオプジェクトに変換する処理が必要です。 Socket 通信はリクエストを自分で組み立てないといけなかったり、レスポンスが生デー タのままなので大変ですが、とても自由度が高いのが特徴です。 4.11 HttpURLConnection Socket ではリクエストを自分で組み立てなければなりませんで したが、 HTTP/HTTPS にアクセスする場合もっと便利なクラスが あります。それが HttpURLConnection です。それでは先ほどと同様 http://tomorrowkey.github.io にアクセスしてみましよう ( リスト 4.17 ) 。 リスト 4.17 : HttpURLConnection の実装方法 try { URL url = new URL("http://tomorrowkey ・ github. io ” ) ; HttpURLConnection connection = (HttpURLConnection) url . openConnection ( ) ; 92
第 4 章ネットワークプログラミング 次のように変換された結果がタブ区切りで表示されます ( リスト 4.44 ) 。 リスト 4.44 : Social IME のレスポンス 携帯電話けいたいでんわケイタイデンワ それではこれを Andr 。 id で実装してみましよう リスト 4.45 : Social IME try { String keyword = params [ 0 ] ; ( リスト 4.45 ) 。 URL url = new URL( ” http://www.social-ime.com/api/?string=" + keyword) ; HttpURLConnection connection = (HttpURLConnection) url. openConnection ( ) ; connection. connect() ; lnputStream inputStream = connection. getlnputStream() ; StringBui1der sb = new StringBui1der() ; int length ; byte[] buffer = new byte[1024] ; while ( (length = inputStream. read(buffer) ) ! = sb ・ append(new String(buffer, 0 , length, "EUC-JP ” ) ) ; return sb. t0String() ; } catch (IOException e) { throw new RuntimeException (e) ; 基本的には前述した HttpURLConnection を使ったアクセスと同じです。 気をつけなければならないのは、 byte 配列から文字列にする際に指定する文字 コードです。ソーシャル IME では特に指定しなければレスポンスは "EUC-JP " という文字コードでレスポンスを返すプロトコルを採用しています。受け取った byte 配列からそのまま文字列を生成すると文字化けを起こしてしまうので、文 字コードを指定します。 WebAPI を使う際に文字化けを起こしてしまうことは 104 合わせてリクエストを組み立て、レスポンスをパースしてください。 WebAPI ごとにリクエスト、レスポンスのフォーマットが異なるため、仕様に よくあるので注意しましよう。
4.11 HttpURLConnection String contentLength = connection. getHeaderFie1d( "Content-Length" ) ; Log. d( ” TEST ” , ” Content-Length=" + contentLength) ; connection. getHeaderFie1d("Content-Type") ; String contentType Log. d("TEST" ” = ”十 contentType) ; contentType レスポンスボディを取得する場合、 getInputStream() メソッドを呼びます ( リ 実行結果は次のとおりです ( リスト 4.25 ) 。 4.11.3 実行結果 めメソッド化しています。 InputStream から文字列に変換する処理は Socket と同じなので、効率化のた "body=" + body) ; Log. d("TEST" String bOdy = readToEnd (inputStream) ; lnputStream inputStream = connection. getInputStream() ; リスト 4.24 : レスポンスボディの取得 スト 4.24 ) 。 リスト 4.25 : HttpURLConnection を使ったリクエストのレスポンス D/TEST D/TEST D/TEST D/TEST D/TEST D/TEST D/TEST D/TEST D/TEST D/TEST D/TEST D/TEST D/TEST D/TEST 1231 ) : 1231 ) : 1231 ) : ( 1231 ) : 1231 ) : 1231 ) : 1231 ) : 1231 ) : 1231 ) : 1231 ) : 1231 ) : 1231 ) : 1231 ) : 1231 ) : responseC0de=200 Content-Length=nu11 contentType=text/htm1 ; body= く html> く ! DOCTYPE html> く html lang="j a"> く head> charset=utf-8 く title>tomorrowkey GitHub page く /title> く meta charset="UTF-8't / > く /head> く body> く hl > He110 , tomorrow! ! く / hl > く /body> く /html> Socket とは異なり HTTP プロトコルを楽に使えるようなメソッドが用意され 95
第 4 章ネットワークプログラミング なので HttpURLConnection にパース ( 変換 ) しなければならないということ です。 リクエストはファイルの取得なので、 setRequestMethod() メソッドで "GET" を渡します。また、 setRequestProperty() メソッドを使うことでリクエストヘッ ダーを設定できます。 Host は設定しなくても自動的に追加させるのですが、説 明上、あえて追加しました ( リスト 4.20 ) 。 リスト 4.20 : リクエストの設定 connection. setRequestMethod( ” GET") ; connection. setRequestProperty("Host ” "tomorrowkey ・ github. io") ; connect メソッドでサーバと接続します ( リスト 4.21) 。 リスト 4.21 : サーバとの接続 connection. connect ( ) ; 4.11.2 レスポンスを受け取る ステータスコードを取得する場合、 getResponseCode() メソッドを呼びます ( リスト 4.22 ) 。 リスト 4.22 : ステータスコードの取得 connection. getResponseC0de() ; int responseCode ttresponsecode=" 十 responseCode) ; Log. d("TESTtt レスポンスへッダを取得する場合、 getHeaderField() メソッドを呼びます ( リ スト 4.23 ) 。 リスト 4.23 : レスポンスへッダの取得 94
4.10.2 リクエストの組み立て . 4.10.3 レスポンスを受け取る . 4.10.4 実行結果 . HttpURLConnection 4.11 4.11.1 リクエストを送信する . 4.11.2 レスポンスを受け取る . 4.11.3 実行結果 . HttpCIient (Apache Http) 4.12 4.12.1 リクエストを送信する . 4.12.2 レスポンスを受け取る . 4.12.3 実行結果 . ライプラリを使ったネットワーク通信 4.13 4.13.1 リクエストを送信する . 4.13.2 レスポンス . WebAPI にアクセスする 4.14 ソーシャル IME を使ってみよう . 4.15 まとめ 4.16 第 5 章 BIuetooth 通信 106 5.1 Bluetooth 通信の基礎知識 . 106 5.2 アプリケーションで Bluetooth 機能を使う 107 自端末の Bluetooth 設定制御 (BluetoothAdapter) 川 8 BIuetooth 対応の外部デバイスの検索 (BluetoothAdapter) 5.4 109 5.5 ペアリング済みデバイスの取得 (BIuetoothAdapter) 111 5.6 検索されたデバイスに接続 ( クライアント端末として振る舞う ) 112 自端末を別の B ⅲ etooth 機器から発見可能にする 5.7 113 別の BIuetooth 機器からの接続要求を受ける ( サーバー端末と 5.8 して振る舞う ) 5.9 プロファイルのサポート 5.9.1 BluetoothProfiIe インターフェースの ServiceListener 5.9.2 プロファイル固有のオプジェクト 5.10 Bluetooth Low Energy 5.10.1 BluetoothManager 0 1 1 っ 4 0 -4 一りっ 0 冖ー ( 8 一一 0 一 -1 っ 1 つけ 1 -1 1 1 1 115 116 117 118 123 124 5