- Android
-
2012-01-11
org.apache.httpクラスを使ってHTTP通信をおこないます。
AndroidManifest.xmlにインターネット接続許可の記述を追加します。
<uses-permission android:name="android.permission.INTERNET" />
Getメソッド
public String doGet( String url )
{
try
{
HttpGet method = new HttpGet( url );
DefaultHttpClient client = new DefaultHttpClient();
// ヘッダを設定する
method.setHeader( "Connection", "Keep-Alive" );
HttpResponse response = client.execute( method );
int status = response.getStatusLine().getStatusCode();
if ( status != HttpStatus.SC_OK )
throw new Exception( "" );
return EntityUtils.toString( response.getEntity(), "UTF-8" );
}
catch ( Exception e )
{
return null;
}
}
7,245 views




