Android/HttpRequest通信/HttpURLConnection
提供: 初心者エンジニアの簡易メモ
サンプル
同期処理のみ(AsyncTaskなどで実行すること)
String strGetUrl = "https://api.github.com/feeds"; HttpURLConnection urlConn = null; InputStream in = null; BufferedReader reader = null; try { URL url = new URL(strGetUrl); urlConn = (HttpURLConnection) url.openConnection(); urlConn.setRequestMethod("GET"); // urlConn.setRequestMethod("POST"); urlConn.connect(); int status = urlConn.getResponseCode(); Log.i("HttpURLConnection","status_code=" + status); if (status == HttpURLConnection.HTTP_OK) { in = urlConn.getInputStream(); reader = new BufferedReader(new InputStreamReader(in)); StringBuilder output = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { output.append(line); } Log.i("HttpURLConnection", "res=" + output.toString()); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (reader != null) { reader.close(); } if (urlConn != null) { urlConn.disconnect(); } } catch (IOException e) { e.printStackTrace(); } }