在android中使用xml調用webservice,實現自己的單詞查詢

關於webservice的相信介紹,可以到網上查到,現在使用android實現自己的詞典。輸入自己想要查詢的詞語之後,可以獲取單詞的含義。

爲了調用webservice,我們首先需要解析相應的webservice爲我們提供的接口,下面是以post發送請求的案例

POST /webservices/EnglishChinese.asmx HTTP/1.1
Host: fy.webxml.com.cn
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <TranslatorString xmlns="http://WebXml.com.cn/">
      <wordKey>mykey</wordKey>
    </TranslatorString>
  </soap12:Body>
</soap12:Envelope>
其中Content-length的長度是我們發送的實體數據的長度,也就是解析後的xml數據的長度,xml中<wordKey>的值就是我們需要查詢的單詞,我們需要對xml進行操作,將string替換爲我們需要查詢的單詞,在我們提交請求之後,我們需要對webservice的相應進行處理,下面是webservice 響應的格式

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

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfString xmlns="http://WebXml.com.cn/">
  <string>string</string>
  <string>string</string>
</ArrayOfString>

其中string就是webservice爲我們提供的結果,我們需要對返回的xml數據進行解析,拿到結果。

下面首先解析我們發送請求的xml文件 ,因爲其中的命名空間解析起來相對麻煩,因此我把這個xml文件放在src目錄下,然後以流的形式獲取該xml,之後使用BufferedReader對其進行包裝,讀取流中的內容,獲取到改xml內容的字符串表示

public String getSOAPXMLFromLocal() throws Exception{
		InputStream ins = this.getClassLoader().getResourceAsStream("soap.xml");
		BufferedReader br = new BufferedReader(new InputStreamReader(ins));
		String result = new String();
		
		String line = null;
		while((line = br.readLine()) != null){
			result += line;
		}
		br.close();
		return result.trim();
		
	}
   在獲取了soap.xml的字符串表示之後,我們需要將其中<wordkey>的值換成我們需要查詢的單詞,然後打開webservice的URL,用post方式將我們的請求和請求的參數提交,最後從連接的輸入流中得到webservice的響應,從而得到翻譯的結果。

這是輸入流中,webservice響應的xml的示例,可以參照這個案例來解析其中xml

<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://WebXml.com.cn/">
<string>你好</string>
<string>nǐ hǎo</string>
<string/>
<string>hello;how are you</string>
<string/>
</ArrayOfString>

下面是我發送請求和處理結果的類

public class RquestHandler {
	
	public  static String sendQueryRequest(String word,String soap){

		
		try{
		
		soap = soap.replaceAll("mykey", word); //將我們要查詢的單詞替換進去
	
		byte [] entity = soap.getBytes();
		
		HttpURLConnection  con = (HttpURLConnection) new URL("http://fy.webxml.com.cn/webservices/EnglishChinese.asmx").openConnection();
		con.setRequestMethod("POST");
		con.setDoOutput(true);
		con.setConnectTimeout(500);
		con.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
		con.setRequestProperty("Content-Length",String.valueOf(entity.length));
		con.getOutputStream().write(entity);
		if(con.getResponseCode() == HttpURLConnection.HTTP_OK){
			String result = getResultFromInputStream(con.getInputStream());
			return result;
			}
		}catch(Exception  e){
			
		}
		return null;
	}
	
	
	public  static String getResultFromInputStream(InputStream ins ) throws Exception{
		
		StringBuilder result = new StringBuilder();
		
		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(ins, "utf-8");
		
		int deepth = 0;  //用來標識解析到了那個string標籤,因爲每個string標籤都用相應的含義。
		
		
		int eventType = parser.getEventType();
		
		while(eventType != XmlPullParser.END_DOCUMENT){
			switch(eventType){
			case XmlPullParser.START_TAG:
				if("string".equals(parser.getName())){
					
				
					
					if(deepth == 1){
						result.append("發音 : "+parser.nextText()+"\n");
					}
					
					if(deepth == 3){
						result.append("網絡翻譯 : "+parser.nextText()+"\n");
					}		
	
					if(deepth == 4){
						result.append("mp3地址  : "+parser.nextText());
					}
					deepth ++ ;
				}
				break;
			}
			eventType = parser.next();
		}
		return result.toString();
	}
}

 

文章系原創,轉載請註明出處,如有疑問請聯繫QQ:453973206



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章