Android:HttpURLConnectionの自分用メモ

  • setRequestPropertyでヘッダーに書き込む
  • queryはurlのあとに入れる(?key=value&key2=value2)
  • inputstreamで内容取得
try {
	URL url = new URL("http://www.yahoo.co.jp/");
	HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
	httpURLConnection.setConnectTimeout(30 * 1000);//ms
	httpURLConnection.setReadTimeout(90 * 1000);//ms
	httpURLConnection.setRequestMethod("GET");//GET,POST
//	httpURLConnection.setRequestProperty("field","newValue");
	BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
	String line;
	StringBuilder stringBuffer=new StringBuilder();
	while ((line=bufferedReader.readLine())!=null){
		stringBuffer.append(line).append("\n");
	}
	return stringBuffer.toString();
} catch (IOException e) {
//	e.printStackTrace();
	return null;
}

スポンサーリンク

シェアする

フォローする

スポンサーリンク