HTTP通信

AndroidでHTTP通信する方法。
こんな感じ。

#codeprettify{{

 private static CookieStore store = null;

・・・

 DefaultHttpClient client = new DefaultHttpClient();
 HttpParams params = client.getParams();
 HttpConnectionParams.setConnectionTimeout(params, 5000);
 HttpConnectionParams.setSoTimeout(params, 10000);
 HttpGet method = new HttpGet(url);
 // クッキーの設定。クッキーの内容を引き継ぎたい場合に、前回の接続した
 // 時のCookieStoreのインスタンスを再利用する。
 client.setCookieStore(store);
 method.setHeader( "Connection", "Keep-Alive" );
 HttpResponse response = null;
 String res = null;
 try {
   response = client.execute( method );
   // 次の接続時もCookieを使うために退避
   store = client.getCookieStore();
   int status = response.getStatusLine().getStatusCode();
   if ( status == HttpStatus.SC_OK ){
     res = EntityUtils.toString( response.getEntity(), "UTF-8" );
   }
 } catch (ClientProtocolException e) {
 } catch (IOException e) {
 }

}}

参考にしたサイトのURLがわからなくなってしまった・・・


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2011-11-30 (水) 14:39:01