カテゴリー
SugiBlog Webデザイナー・プログラマーのためのお役立ちTips

Webサービスとの連携

この記事は最終更新日から1年以上経過しています。

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;
    }
}


Postメソッド

public String doPost( String url, String params )
{
    try
    {
        HttpPost method = new HttpPost( url );

        DefaultHttpClient client = new DefaultHttpClient();

        // POST データの設定
        StringEntity paramEntity = new StringEntity( params );
        paramEntity.setChunked( false );
        paramEntity.setContentType( "application/x-www-form-urlencoded" );
        method.setEntity( paramEntity );

        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;
    }
}

データのやり取りはXMLよりJSONのほうが早いらしいので、JSONを使用します。

{"subscriptions":[{"id":"feed/http://feeds.feedburner.jp/cnet/rss","title":"CNET Japan","categories":[],"sortid":"BB146333","firstitemmsec":"1230135778214"},{"id":"feed/http://feeds.feedburner.jp/netafull","title":"[N]ネタフル","categories":[{"id":"user/13019141600096008010/label/News","label":"News"}],"sortid":"701D2BE4","firstitemmsec":"1262173951833"}]}


class JSubscriptionsHandler
{
    public List<TagObject> entries;

    public JSubscriptionsHandler() {
    }

    public boolean parse( String istring ) {
        JSONArray jArray;

        try
        {
            jArray = new JSONObject( istring ).getJSONArray( "subscriptions");

            int max = jArray.length();
            entries = new ArrayList<TagObject>( max );

            for ( int i = 0; i < max; i++ )
            {
                JSONObject jsonObj = jArray.getJSONObject(i);

                if( jsonObj.getJSONArray("categories").length() > 0 )
                    continue;

                entries.add( new TagObject(
                        -1, 0,
                        jsonObj.getString("title"),
                        "0",
                        jsonObj.getString("sortid"),
                        jsonObj.getString("id") ));
            }

            return true;
        } catch (JSONException e) {
            Log.d("NetaShare", e.getMessage());
            return false;
        }

    }
}
この記事がお役に立ちましたらシェアお願いします
6,725 views

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です