Android訪問WebService的兩種方法

首先解釋一下WebService:WebService是一種基於SOAP協議的遠程調用標準。經過WebService能夠將不一樣操做系統平臺,不一樣語言、不一樣技術整合到一塊兒。詳細見:http://baike.baidu.com/view/837392.htm#sub837392android

Android中訪問WebService總結有兩種:一、經過連接 二、經過第三方類庫網絡

先說說第一種:app

比較簡單貼代碼了:svn

final String SERVER_URL = "http://192.168.1.55/PosWebServices/WebUI.asmx"; // 定義須要獲取的內容來源地址 URL url = new URL(SERVER_URL); URLConnection con = url.openConnection();ui

//一些請求設置 con.setDoOutput(true); con.setRequestProperty("Pragma:", "no-cache"); con.setRequestProperty("Cache-Control", "no-cache"); con.setRequestProperty("Content-Type", "text/xml");
OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());google

out.write(new String(xmlInfo.getBytes("UTF-8"))); //這裏能夠發參數的,字符串應該是XML格式的 out.flush(); out.close();url

 

// 取返回值操作系統

BufferedReader br = new BufferedReader(new InputStreamReader(con .getInputStream())); StringBuilder sBuilder = new StringBuilder(); String line = ""; for (line = br.readLine(); line != null; line = br.readLine()) { sBuilder.append(line); } // 解析XML Pattern patternname = Pattern.compile("<Name>.*?</Name>"); Matcher matchername = patternname.matcher(sBuilder.toString()); if (matchername.find()) { String name = matchername.group(); TextView lblname = (TextView) findViewById(R.id.lbl_name); lblname.setText(URLDecoder.decode(name.substring(name .indexOf(">") + 1, name.lastIndexOf("<")))); }
Pattern patternage = Pattern.compile("<Age>.*?</Age>"); Matcher matcherage = patternage.matcher(sBuilder.toString()); if (matcherage.find()) { String age = matcherage.group(); TextView lblage = (TextView) findViewById(R.id.lbl_age); lblage.setText(age.substring(age.indexOf(">") + 1, age .lastIndexOf("<"))); }
} catch (Exception e) { String str = e.getMessage(); }code

很簡單 很少說了 還能夠用HttpPost加HttpResponse的方式。xml

第二種:

須要下載一個第三方Jar包:ksoap2

下載地址:http://ksoap2-android.googlecode.com/svn-history/r575/m2-repo/com/google/code/ksoap2-android/ksoap2-android-assembly/2.5.4/ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar

相比舊版本加強了網絡連接等等同時用HttpTransportSE 替代了AndroidHttpTransport ,建議用新版本

下載後導入工程,很少解釋了

1. 指定WebService的命名空間和調用的方法名,代碼以下:

SoapObject request = new SoapObject("http://service", "getName");

 

2. 設置調用方法的參數值,若是方法沒有參數,能夠省略這一步。設置方法的參數值的代碼以下:

request.addProperty("m1", "v1");

 

request.addProperty("m2", "v2");

 

3. 生成調用WebService方法的SOAP請求信息。該信息由SoapSerializationEnvelope對象描述,代碼以下: SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10); envelope.bodyOut = request; 若是是.NET的WebService還需加:

 

4. 建立HttpTransportSE對象。經過HttpTransportSE類的構造方法能夠指定WebService的WSDL文檔的URL,代碼以下:

HttpTransportSE ht = new HttpTransportSE("http://192.168.1.55/PosWebServices/WebUI.asmx?wsdl");

HttpTransportSE.dotNet=true;

 

5. 使用call方法調用WebService方法,代碼以下: ht.call(null, envelope); call方法的第1個參數通常爲null,第2個參數就是在第3步建立的SoapSerializationEnvelope對象。 6. 使用getResponse方法得到WebService方法的返回結果,代碼以下: SoapObject soapObject = (SoapObject) envelope.getResponse();

 

7.取值

soapObject.getProperty("這個名字你懂的");

相關文章
相關標籤/搜索