在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



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