ÊÙ¶¯²ñ

Android¤Çorg.apache.http.client¤ò»ÈÍѤ¹¤ë

  • ¢¨¸½ºß¥ì¥Ó¥å¡¼Ãæ¤Ç¤¹¡£ÆâÍƤϴְ㤤¤ò´Þ¤ó¤Ç¤¤¤ë²ÄǽÀ­¤¬¤¢¤ê¤Þ¤¹¡£

Android¤ÇHTTP¤ò»ÈÍѤ·¤Æ¥Í¥Ã¥È¥ï¡¼¥¯¤Ë¥¢¥¯¥»¥¹¤¹¤ë¤Ë¤Ï¡¢org.apache.http.client¥Ñ¥Ã¥±¡¼¥¸¤Î¥¯¥é¥¹¤ò»ÈÍѤ·¤Þ¤¹¡£

¤¿¤À¤·¡¢»È¤¤¤³¤Ê¤¹¤¿¤á¤Ë¤ÏHTTP¥×¥í¥È¥³¥ë¤ò¿¾¯Íý²ò¤·¤Æ¤ª¤¯É¬Íפ¬¤¢¤ê¡¢Æȼ«¤Ë¥Ø¥Ã¥À¡¼¤òÀßÄꤹ¤ë¤Ê¤É¡¢Æüì¤Ê»È¤¤Êý¤ò¤¹¤ë¤Î¤Ç¤Ê¤±¤ì¤Ðorg.apache.http.client¤òľÀÜÁàºî¤¹¤ëɬÍפϤ¢¤Þ¤ê¤¢¤ê¤Þ¤»¤ó¡£

¤³¤³¤Ç¤ÏandroidÍѤΥ桼¥Æ¥£¥ê¥Æ¥£¤È¤·¤Æ¥ª¡¼¥×¥ó¥½¡¼¥¹¤Ç¸ø³«¤µ¤ì¤Æ¤¤¤ëRestfulClient¥¯¥é¥¹¤ò»ÈÍѤ·¡¢HTTP¤ÎGET¥á¥½¥Ã¥É¤ò»È¤Ã¤Ægoogle¤Ë¸¡º÷¥¯¥¨¥ê¤òÅꤲ¤Æ·ë²Ì¤òHTML¤Ç¼õ¤±¼è¤Ã¤Æ¤ß¤¿¸å¡¢RestfulClient¥¯¥é¥¹Æâ¤ÎÆ°ºî¤òÄɤäƤߤޤ·¤ç¤¦¡£

¼ê½ç1¡§¸ø³«¤µ¤ì¤Æ¤¤¤ëandroidÍѤΥ桼¥Æ¥£¥ê¥Æ¥£¤Î¥½¡¼¥¹¥Õ¥¡¥¤¥ë¤ò¥À¥¦¥ó¥í¡¼¥É¤·¡¢Eclipse¤ËÅÐÏ¿¤¹¤ë

  • ¥½¡¼¥¹¥Õ¥¡¥¤¥ë¤ÏGitHub¤È¤¤¤¦¥µ¡¼¥Ó¥¹¤ò»È¤Ã¤Æ¸ø³«¤µ¤ì¤Æ¤¤¤Þ¤¹¤Î¤Ç¡¢GitHub¾å¤Îandroid_utils¥×¥í¥¸¥§¥¯¥È¤ò³«¤­¡¢download¥Ü¥¿¥ó¤«¤éZip¥Õ¥¡¥¤¥ë¤ò¥À¥¦¥ó¥í¡¼¥É¤·¤Þ¤¹¡£ (Git¤ò»È¤¨¤ë¿Í¤Ïgit clone¤·¤Æ¤â¤é¤Ã¤¿Êý¤¬º£¸å³Ú¤«¤â¤·¤ì¤Þ¤»¤ó)
    httpclient.download_from_github.jpg
  • Eclipse¤ò³«¤­ "File -> Import"¤òÁªÂò¤·¤Þ¤¹¡£
  • "General¤ÎÃæ¤ÎExisting Projects into workspace"¤òÁªÂò¤·¤Æ¡¢"Next"¥Ü¥¿¥ó¤ò²¡¤·¤Þ¤¹¡£
    httpclient.Importtype.jpg
  • ¥À¥¦¥ó¥í¡¼¥É¤·¤¿Zip¥Õ¥¡¥¤¥ë(¤â¤·¤¯¤Ïgit clone¤·¤¿¥Ç¥£¥ì¥¯¥È¥ê)¤«¤é¡¢°Ê²¼¤Î¥¹¥¯¥ê¡¼¥ó¥·¥ç¥Ã¥È¤Î¤è¤¦¤Ëandroid_utils¥×¥í¥¸¥§¥¯¥È¤ò¥¤¥ó¥Ý¡¼¥È¤·¤Þ¤¹¡£°ì½ï¤Ëandroid_util_tests¥×¥í¥¸¥§¥¯¥È¤â¥¤¥ó¥Ý¡¼¥È¤·¤Æ¤ª¤¤¤¿Êý¤«¸å¡¹³Ú¤«¤âÃΤì¤Þ¤»¤ó¡£
    httpclient.ImportFrom.jpg
  • Eclipse¤Ë¥×¥í¥¸¥§¥¯¥È¤¬¥¤¥ó¥Ý¡¼¥È¤µ¤ì¤Æ¤¤¤ë¤³¤È¤ò³Îǧ¤·¤Þ¤¹¡£

¼ê½ç2¡§¥×¥í¥¸¥§¥¯¥È¤òºîÀ®¤·¤Þ¤·¤ç¤¦

google¤Ë¸¡º÷¥¯¥¨¥ê¤òȯ¹Ô¤¹¤ë¤¿¤á¤Î¥×¥í¥¸¥§¥¯¥È¤òºîÀ®¤·¤Þ¤¹¡£

  • "File -> New -> Project"¤òÁªÂò¤·¤Æ¡¢³«¤¤¤¿¥À¥¤¥¢¥í¥°¤Ç"Android Poroject"¤òÁªÂò¤·¤Æ¡¢"Next"¥Ü¥¿¥ó¤ò²¡¤·¤Þ¤¹¡£
  • ³«¤¤¤¿¥À¥¤¥¢¥í¥°¤Ç¡¢°Ê²¼¤Î¤è¤¦¤ËÆþÎϤ·¤ÆFinish¥Ü¥¿¥ó¤ò²¡¤·¤Þ¤¹¡£
    • Project Name: HelloHTML
    • Package name: net.it4myself.hellohtml
    • Activity Name: .HelloHTML
    • Application Name: ¥Ï¥í¡¼HTML

¼ê½ç3¡§¥Þ¥Ë¥Õ¥§¥¹¥È¥Õ¥¡¥¤¥ë¤òÀßÄꤹ¤ë

  • ¤³¤Î¥×¥í¥¸¥§¥¯¥È¤Ç¤Ï¥¤¥ó¥¿¡¼¥Í¥Ã¥È¤Ë¥¢¥¯¥»¥¹¤¹¤ë¤¿¤á¡¢¥Ñ¡¼¥ß¥Ã¥·¥ç¥ó¤Î»ÈÍѤòµö²Ä¤¹¤ëɬÍפ¬¤¢¤ê¤Þ¤¹¡£
  • ¤â¤¦´·¤ì¤Þ¤·¤¿¤Í?¡¡¥½¡¼¥¹¤À¤±Å½¤Ã¤Æ¤ª¤­¤Þ¤¹¡£
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="net.it4myself.hellohtml"
      android:versionCode="1"
      android:versionName="1.0.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".HelloHTML"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.INTERNET"/>
</manifest> 

¼ê½ç4¡§¥ì¥¤¥¢¥¦¥È¤ò·èÄꤹ¤ë

²èÌ̤Υ쥤¥¢¥¦¥È¤ò·èÄꤹ¤ë¤¿¤á¤Ë¡¢res/layout/main.xml¤òÊÔ½¸¤·¤Þ¤·¤ç¤¦¡£

  • ¤³¤ì¤âº£²ó¤Î¥Á¥å¡¼¥È¥ê¥¢¥ë¤Î¼ñ»Ý¤«¤é³°¤ì¤ë¤Î¤Ç¡¢¥½¡¼¥¹¤À¤±Å½¤Ã¤Æ¤ª¤­¤Þ¤¹¡£
  • ¤Á¤Ê¤ß¤ËDroidDraw¤Çºî¤Ã¤¿¤â¤Î¤Ç¤¹¡£¤Á¤Ê¤ß¤Ë¤Á¤Ê¤ß¤Ë¡¢¥¹¥¿¥ó¥É¥¢¥í¡¼¥óÈǤÎÊý¤¬¹âµ¡Ç½¤Ç¤¹!
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  xmlns:android="http://schemas.android.com/apk/res/android"
>
<LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
>
<TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:hint="Query String"
>
</TextView>
<EditText
  android:id="@+id/query_text"
  android:layout_width="200px"
  android:layout_height="wrap_content"
  android:text="EditText"
  android:textSize="18sp"
>
</EditText>
<Button
  android:id="@+id/search_button"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="search"
>
</Button>
</LinearLayout>
<TextView
  android:id="@+id/html_source"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="TextView"
>
</TextView>
</LinearLayout> 

¼ê½ç5¡§¼ê½ç1¤Çºî¤Ã¤¿¥×¥í¥¸¥§¥¯¥È¤ò¥Ó¥ë¥É¥Ñ¥¹¤Ë²Ã¤¨¤ë

ÊÌ¤Î¥×¥í¥¸¥§¥¯¥ÈÆâ¤Ë¤¢¤ë¥Ñ¥Ã¥±¡¼¥¸¤ò»ÈÍѤ¹¤ë¤Î¤Ç¡¢¥Ó¥ë¥É¥Ñ¥¹¤ÎÀßÄê¤ò¤¹¤ëɬÍפ¬¤¢¤ê¤Þ¤¹¡£¤³¤ì¤ò˺¤ì¤Æ¤·¤Þ¤¦¤È¡¢net.it4myself.util.RestfulClient¥¯¥é¥¹¤Ï»ÈÍѤǤ­¤Þ¤»¤ó¡£

  • HelloHTML¥×¥í¥¸¥§¥¯¥È¾å¤Ç±¦¥¯¥ê¥Ã¥¯¤·¤Æ¡¢"Properties"¤òÁªÂò¤·¤Þ¤¹
  • ɽ¼¨¤µ¤ì¤¿¥À¥¤¥¢¥í¥°¥Ü¥Ã¥¯¥¹¤Ç"Java Build Path"¤òÁªÂò¤·¤Þ¤¹(­¡)
  • "Projects"¥¿¥Ö¤òÁªÂò¤·¤Þ¤¹(­¢)
  • "Add..."¥Ü¥¿¥ó¤ò¥¯¥ê¥Ã¥¯¤·¡¢"android_utils"¥×¥í¥¸¥§¥¯¥È¤òÁªÂò¤·¤Þ¤¹(­£)
  • "android_utils"¤¬Äɲ䵤줿¤³¤È¤ò³Îǧ¤·¤Þ¤¹(­¤)
  • OK¥Ü¥¿¥ó¤ò¥¯¥ê¥Ã¥¯¤·¤ÆÀßÄê¤ò´°Î»¤·¤Þ¤¹(­¥)
    httpclient.add_build_path.jpg

¼ê½ç6¡§Get¥á¥½¥Ã¥É¤ò»È¤Ã¤Ægoogle¸¡º÷·ë²Ì¤ò¼èÆÀ¤¹¤ë

  • src¤Î²¼¤Înet.it4myself.hellohtml¤ÎHelloHTML.java¤ò³«¤¤¤Æ°Ê²¼¤Î¤è¤¦¤ËÆþÎϤ·¤Þ¤¹¡£
  • °Ê²¼¤Î¥³¡¼¥É¤Ç¤Ï¡¢°Ê²¼¤Î¼ê³¤­¤ò¹Ô¤Ã¤Æ¤¤¤Þ¤¹¡£
    • search_button¥Ü¥¿¥ó¤ÎOnClickListner¤Ë°Ê²¼¤Î¥¤¥Ù¥ó¥È¤ò»Å¹þ¤à
      • "http://www.google.com/search?q="¤Èquery_text¥¨¥Ç¥£¥Ã¥È¥Æ¥­¥¹¥È¤ÎÆþÎÏÆâÍƤòÏ¢·ë¤µ¤»¡¢¥¢¥¯¥»¥¹¤¹¤ëURL¤òÀ¸À®¤¹¤ë
      • RestfulClient.Get()¤ò¸Æ¤Ö¡£Âè°ì°ú¿ô¤ÏURL¡£ÂèÆó°ú¿ô¤Ï¥Ñ¥é¥á¡¼¥¿¤À¤¬¡¢URL¤Ë¥¯¥¨¥ê¡¼¥¹¥È¥ê¥ó¥°¤ò´Þ¤á¤¿¤Î¤Çnull¤È¤·¤Æ¤¤¤ë¡£
      • RestfulClient.Get()¤ÎÊÖÃͤȤ·¤Æ¸¡º÷·ë²Ì¤ÎHTML¤¬Ê֤äƤ¯¤ë¤Î¤Ç¡¢html_source¥Æ¥­¥¹¥È¥Ó¥å¡¼¤Ë¥»¥Ã¥È¤¹¤ë¡£
package net.it4myself.hellohtml;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException; 

import net.it4myself.util.RestfulClient;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView; 

public class HelloHTML extends Activity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button b = (Button)findViewById(R.id.search_button);
        b.setOnClickListener(new View.OnClickListener() {
           public void onClick(View v) {
            	EditText input = (EditText)findViewById(R.id.query_text); 
		TextView html_source =  (TextView)findViewById(R.id.html_source);
               String queryString = "http://www.google.com/search?q=" + input.getText().toString();
               try {
                   String html = RestfulClient.Get(queryString, null);
                   html_source.setText(html);
               } catch (ClientProtocolException e) {
                   e.printStackTrace();
                   html_source.setText("error1");
               } catch (IOException e) {
                   e.printStackTrace();
                   html_source.setText("error2");
               }
           }
        });
    }
}
  • ¥×¥í¥¸¥§¥¯¥È¤ò±¦¥¯¥ê¥Ã¥¯¤·¤Æ¡¢"Run As"->"Android Application"¤Ç¼Â¹Ô¤·¤Æ¤ß¤Þ¤·¤ç¤¦¡£Query¤ÎÆâÍƤ˱þ¤¸¤Æ¡¢É½¼¨¤µ¤ì¤ëHTML¤ÎÆâÍƤ¬°Û¤Ê¤Ã¤Æ¤¤¤ë¤Ï¤º¤Ç¤¹¡£

RestfulClient.Get()¤ÎÆâÍƤò³Îǧ¤¹¤ë

shared_android_utils¥×¥í¥¸¥§¥¯¥È¤ò³«¤­¡¢src¤Î²¼¤Înet.it4myself.util¤ÎRestfulClient.java¤ò³«¤¤¤ÆÆâÍƤò³Îǧ¤·¤Þ¤¹¡£ RestfulClient¥¯¥é¥¹¤Î¾ì¹ç¡¢ÊÖÃͤ¬String¤«DOM¥ª¥Ö¥¸¥§¥¯¥È¤«¤Ë¤è¤Ã¤Æ¥ª¡¼¥Ñ¥í¡¼¥É¤µ¤»¤Æ¤¤¤Þ¤¹¤¬¡¢´ðËÜŪ¤Êή¤ì¤ÏƱ¤¸¤Ç¤¹¡£¤³¤³¤Ç¤Ï´ðËÜŪ¤Êή¤ì¤ò²òÀ⤷¤Þ¤¹¡£

  • (A) HTTP¥×¥í¥È¥³¥ë¤ò»È¤Ã¤Æ¥¢¥¯¥»¥¹¤¹¤ë¾ì¹ç¡¢ºÇ¤â´Êñ¤Ê¤Î¤Ïorg.apache.http.impl.client¥Ñ¥Ã¥±¡¼¥¸¤ÎDefaultHttpClient¥¯¥é¥¹¤ò»ÈÍѤ¹¤ë¤³¤È¤Ç¤¹¡£DefaultHttpClient¥¯¥é¥¹¤ÏHTTP¥×¥í¥È¥³¥ë¤ËÂбþ¤·¡¢(RestfulClinet¥¯¥é¥¹¤Ç¤Ï»ÈÍѤ·¤Æ¤¤¤Þ¤»¤ó¤¬)SSL¤ä¥Ù¡¼¥·¥Ã¥¯Ç§¾Ú¤Î¤¿¤á¤ÎÀßÄê¤Ê¤É¤ò¹Ô¤¦¤³¤È¤¬½ÐÍè¤Þ¤¹¡£RestfulClinet¥¯¥é¥¹¤Ç¤ÏDoRequest()¥á¥½¥Ã¥É¤Ç¤³¤Î¥¤¥ó¥¹¥¿¥ó¥¹¤òÀ¸À®¤·¤Æ¤¤¤Þ¤¹¡£
  • (B) ¤³¤ÎDefaultHttpClient¥¯¥é¥¹¤Îexecute¥á¥½¥Ã¥É¤Ë¡¢HttpGet¤Î¥¤¥ó¥¹¥¿¥ó¥¹¤äHttpPost¤Î¥¤¥ó¥¹¥¿¥ó¥¹¤òÅϤ¹¤³¤È¤Ç¡¢HTTP¥×¥í¥È¥³¥ë¤Î³Æ¥á¥½¥Ã¥ÉÆÃÍ­¤ÎÀßÄê¤ò¹Ô¤¤¤Þ¤¹¡£RestfulClinet¥¯¥é¥¹¤Ç¤ÏDoRequest()¤Î°ú¿ô¤È¤·¤ÆÅϤäƤ­¤Þ¤¹¡£
  • (C) DefaultHttpClient¥¯¥é¥¹¤Îexecute¥á¥½¥Ã¥É¤ÎÌá¤êÃͤϡ¢HttpResponse¤Ç¤¹¡£Ìá¤êÃͤ«¤é¤ÏHTTP¥×¥í¥È¥³¥ë¤Î¥ì¥¹¥Ý¥ó¥¹¥³¡¼¥É(200 OK¤Ê¤É)¤äInputStream¤ò¼è¤ê½Ð¤»¤Þ¤¹¡£RestfulClinet¥¯¥é¥¹¤Ç¤ÏEntity¤ò¼è¤ê½Ð¤·¡¢¤½¤ì¤òEntityUtils.toString()¤ò»È¤Ã¤Æʸ»úÎ󲽤¹¤ë¤«¡¢getDOM()¥á¥½¥Ã¥ÉÆâ¤ÇXML¤È¤·¤Æ¥Ñ¡¼¥¹¤·¤ÆDOM¥ª¥Ö¥¸¥§¥¯¥È¤ò¼è¤ê½Ð¤·¤Æ¤¤¤Þ¤¹¡£
  • ¢¨android ver1.0»þÅÀ¤Ç¤Ïorg.w3c.dom¥Ñ¥Ã¥±¡¼¥¸¤Ï¥Ð¥°¤¬Â¿¤¤¤é¤·¤¯¡¢°ìÉô¥ª¥×¥·¥ç¥ó¤¬Æ°¤¤¤Æ¤¯¤ì¤Ê¤«¤Ã¤¿¤¿¤á¡¢¶õ¥Æ¥­¥¹¥È¥Î¡¼¥Éºï½üÍѤËRemoveEmptyNodes()¤âÍÑ°Õ¤·¤Þ¤·¤¿¡£DOM¥ª¥Ö¥¸¥§¥¯¥È¤ò°·¤¤¤¿¤¤¾ì¹ç¤Ï¤ª»È¤¤¤¯¤À¤µ¤¤¡£ »²¹Í¤Ë¥½¡¼¥¹¤âÄ¥¤Ã¤Æ¤ª¤­¤Þ¤¹¡£
      public static String Get(String uri, HashMap<String,String> map) throws ClientProtocolException, IOException {
        String fulluri;
    
        if(null == map){
          fulluri = uri;
        } else {
          fulluri = uri + packQueryString(map);
        }
        HttpGet method = new HttpGet(fulluri);
        return EntityUtils.toString(DoRequest(method));
      }
      
      private static HttpEntity DoRequest(HttpUriRequest method) throws ClientProtocolException, IOException {
        HttpClient client = new DefaultHttpClient(); // (A)
        HttpResponse response = null;
    		
        try {
          response = client.execute(method); // (B)
          int statuscode = response.getStatusLine().getStatusCode(); // °Ê²¼(C)
    			
          //¥ê¥¯¥¨¥¹¥È¤¬À®¸ù 200 OK and 201 CREATED
          if (statuscode == HttpStatus.SC_OK | statuscode == HttpStatus.SC_CREATED){ 
            return response.getEntity();
          } else {
            throw new HttpResponseException(statuscode, "Response code is " + Integer.toString(statuscode));
          }
        }catch (RuntimeException e) {
          method.abort();
          Log.v(TAG, e.getMessage());
          throw new RuntimeException(e);
        }
      }

ÊÙ¶¯²ñ