ウェブアプリケーション,インジェクション,コマンドインジェクション

Androidアプリ開発 ネットワークプログラミング

XMLウェブサービスを実行

Consuming XML Web service

ここ数年の展開されているほとんどのウェブサービスは、
XMLとは拡張ができる言語で、XMLが現在いろんな所で見られる様に、
たくさんの開発者に選ばれている言語で、
ほとんどすべてのプラットフォームでサポートされています。
ここではXMLの結果を構文解析し、必要な情報を取り出す方法を示します。
どのようにウェブサービスに接続できるか、XMLの結果を構文解析するか
を図解するためには、ここでの例では、
http://services.aonaware.com/DictService/DictServise.asmx
ウェブ辞書サービスを使います。
このウェブサービスでは、言葉の定義をチェックできるようになります。
このウェブサービスは以下のようなアクセスメソッドをサポートしています。
SOAP 1.1、SOAP1.2、HTTP GET、HTTP POST
単純化するために、この例ではHTTP GETを使います。
以下はサーバに送信する必要がある情報と(top part)、
サーバから返される結果(bottom part)を示します。

//---top part---
GET /DictService/DictService.asmx/Define?word=string HTTP/1.1
Host: services.aonaware.com

HTTP/1.1 200 OK
Content-Type text/xml; charset=utf-8
Content-Length: length

//---bottom part---
<?xml version="1.0" encoding="utf-8"?>
<WordDefinition xmlns="http://services.aonaware.com/webservices/">
  <Word>String</Word>
  <Definitions>
   <Definition>
    <Word>string</Word>
    <Dictionary>
     <Id>string</Id>
     <Name>string</Name>
    <Dictionary>
    <WordDefinition>string</WordDefinition>
   </Definition>
   <Definition>
    <Word>string</Word>
    <Dictionary>
     <Id>string>
     <Name>string</Name>
    </Dictionary>
    <WordDefinition>string</WordDefinition>
   </Definition>
  </Definitions>
<WordDefinition>

見ての通り、ウェブサービスはサービスコールの結果を
含むXML文字列を返しています。
特に、言葉の定義はいろんな<WordDefinition>要素の中に
含まれているのがわかります。
このため、与えたタスクは<WordDefinition>要素の
内容を検索することになります。これを行うためには、
以下のようなWordDefinition()メソッドを定義します。

private String WordDefinition(String word){
  InputStream in = null;
  String strDefinition = "";
  try{
   in = OpenHttpGETConnection(
    "http://services.aonaware.com/Dictservice/" +
    "DictService.asmx/Define?word=" + word);
   Document doc = null;
   DocumentBuilderFactory dbf =
     DocumentBuilderFactory.newInstance();
   DocumentBuilder db;
   try{
    db = dbf.newDocumentBuilder();
    doc = db.parse(in);
   }catch (ParserConfigurationException e){
    e.printStackTrace();
   }catch (Exception e){
    e.printStackTrace();
   }
   doc.getDocumentElement().normalize();

   //---retrieve all the <Definition> elements---
   NodeList definitionElement =
    doc.getElementByTagName("Definition");

   //---iterate through each <Definition> element---
   for (int i = 0; i < definitionElements.getLength(); i++){
    Node itemNode = definitionElements.item(i);
    if (itemNode.getNodeType() == Node.ELEMENT_NODE){
     //---convert the Definition node into an Element---
     Element definitionElement = (Element) itemNode;

     //---get all the <WordDefinition> elements under
     //the <Definition> element---
     NodeList wordDefinitionElements =
       definitionElement.getElementsByName("WordDefinition");

     strDefinition = "";
     //---iterate through each <WordDefinition> element---
     for ( int j = 0; j < wordDefinitionElements.getLength(); j++){
      //---get all the child nodes under the <wordDefinition> element---
      NodeList textNodes =
         (wordDefinitionElements.item(j)).getChildNodes();
      strDefinition += ((Node)textNodes.item(0))
         getNodeValue() + ". \n";
     }
    }
   }
  }catch (Exception e){
   Log.d("NetworkingActivity", e.getLocalizedMessage());
  }
  //---return the definitions of the word---
  return strDefinition;
}

このメソッドはOpenHttpGETConnection()メソッドを
HTTP GETを使ってウェブサービスに接続するために呼び出します。
OpenHttpGETConnection()メソッドについては、
HTTP GETを使ってサーバに接続するを参照してください。
OpenHttpGETConnection()メソッドは、DocumentBuilderFactoryと
DocumentBuilderクラスの両方の使用を通してDocumentオブジェクト内の
ウェブサービスからの結果をロードします。
DocumentオブジェクトはDOM(Document Object Model)を使って、
XMLドキュメントを表現します。
要素の中を繰り返すことでXMLの結果の中を構文解析し、
<WordDefinition>要素の内容の位置を見つけています。
すべてのネットワーク操作にあるように、
AsycTaskクラスのサブクラス内部のWordDefinition()メソッドを
呼び出す必要があります。

private class AccessWebServiceTask extends AsyncTask <String, void, String>{
  protected String doInBackground(String... urls){
   return WordDefinition(urls[0]);
  }

  protected void onPostExecute(String result){
   Toast.makeText(getBaseContext(), result, Toast.LENGTH_LONG).show();
 &emps;}
]

最後にウェブサービスにアクセスするために以下のステートメントを
使います。

//---access a Web Service using GET---
new AccessWebServiceTask().execute("cool");
ホーム
便利堂ロゴ
inserted by FC2 system