如題,因為有需求開始研究android 跟.net service串接的部份
網路上很多是用ksoap2來做處理,但是網路上很多的範例都是很簡單型態的,對於使用簡單型態的方式用ksoap2來處理是還ok,但是如果一但是複雜型的話,就需要對每一層的對像做序列化的動作,不是很簡單,所以後來改用比較簡單的方式來處理就是利用httppost的方式來傳輸soap所要的資訊,就簡單多了,也比較方便,往後要改只需要改傳輸的xml部份,不用再重新序列化,廢話不多說,馬上來看程式吧
可以將這個串接動作分成三部份
第一部份組成相應的soap資訊
第二部份利用httppost來傳送給web service
第三部份利用SAXParser來解析所獲得的soap回應訊息
同樣網路上很少有附上完整soap的部份,因為有其他考量所以不提供webservie連結以下以webserviceUrl代表http://xxxxx/xxx.asmx
我所使用的是webservice 中的Test2, 文中Test2代表你所要使用的方法名稱
網頁上獲得的soap規格有1.1跟1.2版,我是利用不分版本的方式來處理的
不分版的語法就是把相關的11,12拿掉即可
在xml之上為header 檔頭所要附的資訊
不分版所要組成的資訊就是
POST xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Host: xxxxxxxx Content-Type: application/soap+xml; charset=utf-8 Content-Length: length <?xml version="1.0" encoding="utf-8"?> <soap: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"> <soap:Body> <Test2 xmlns="Test2 url"> <clientreq> <imgbuf1>base64Binary</imgbuf1> <imgbuf2>base64Binary</imgbuf2> <xxx>string</xxx> </clientreq> </Test2> </soap:Body> </soap:Envelope>
其中比較需要特別注意的是imgbuf1跟imgbuf2所代表的base64Binary代表你需要將資訊轉為base64字串, google一下可以找到相關的base64.java加到你的專案中
下面提供一個可以比對你所轉出來的是否為base64的網址:http://www.motobit.com/util/base64-decoder-encoder.asp
可以利用此比對你的資訊是否正確
以下就來看程式如何寫吧
第一部份:
private String getRequestXml(){
String imgbuf1 = "";
String imgbuf2 = "";
try {
StringBuilder sb = new StringBuilder("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
//sb.append("<soap:Header/>");
sb.append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");
sb.append("<soap:Body>");
sb.append("<Test2 xmlns=\"Test2 url\">");
sb.append("<clientreq>");
sb.append("<imgbuf1>");
sb.append(imgbuf1);
sb.append("</imgbuf1>");
sb.append("<imgbuf2>");
sb.append(imgbuf2);
sb.append("</imgbuf2>");
sb.append("<xxx>string</xxx>");
sb.append("</clientreq>");
sb.append("</Test2>");
sb.append("</soap:Body>");
sb.append("</soap:Envelope>");
return sb.toString();
}
PS:imgbuf1,imgbuf2,請自行利用base64來取得
第二部份:
private String httpClientPost() {
String result = "";
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("webserviceUrl");
post.setHeader("Content-Type", "text/xml;charset=utf-8");
try {
StringEntity se = new StringEntity(getRequestXml(), HTTP.UTF_8);
post.setEntity(se);
HttpResponse response = client.execute(post);
Log.i(TAG, "status:"+response.getStatusLine().getStatusCode());
result = EntityUtils.toString(response.getEntity());
} catch (IOException e) {
Log.e(TAG, "Exception:"+e.getMessage());
e.printStackTrace();
}
return result;
}
PS:如果在執行時發現httpPost 回傳415錯誤代表你的Content-type設錯了,請改為text/xml喔, 這個問題讓我卡了不少時間才解決,分享給大家
參考網頁的回應訊息為
HTTP/1.1 200 OK 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> <Test2Response xmlns="Test2 url"> <Test2Result> <Message>string</Message> </Test2Result> </Test2Response> </soap12:Body> </soap12:Envelope>
所以我們要得到的是Message這個Tag的文字內容
第三部份:
請自行利用saxParser自行取得message中的內容,對SAXParser 不了解的可以google一下喔,在此不做說明了
大致程式都說明完畢了,希望大家可以很順利的完成串接的部份喔,請不要在主執行緒執行不然程式會卡住喔,個人習慣是利用AsyncTask來完成此動作
對AsyncTask不了解的可以參考我的另一篇喔
http://jjnnykimo.pixnet.net/blog/post/30990203