Android調用webService

來源:互聯網
上載者:User

標籤:internet   bbb   pull   jar   rac   mission   one   response   sdn   

Android調用WebService

首先你需要的東西。一個ksoap的jar包。下面提供了。

http://download.csdn.net/download/ty0038/10116757

下載jar包。複製項目lib檔案夾下。右擊jar包->add as library... 點擊OK。

首先在需要在Manifest裡面註冊許可權,以免後面忘記!

<uses-permission android:name="android.permission.INTERNET" />

接下來還是貼代碼吧!代碼中有注釋

/*
* 天氣webService
* */
public void tianqi(){
//命名空間
    String namespace = "http://WebXml.com.cn/";
String transUrl = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";
String method = "getSupportCity";
int envolopeVersion = SoapEnvelope.VER12;

String soapAction = "http://WebXml.com.cn/getSupportCity";
//指定webservice的命名空間和調用方法
    SoapObject request = new SoapObject(namespace,method);
//提交需要提交的參數
request.addProperty("byProvinceName","湖南");
//生產調用webService方法的SOAP請求資訊,並指定SOAP的版本 ,這個版本 根據 asdl檔案中是1.1還是1.2協議來指定的. 一般VER11或者VER12都可以,以前還有VER10
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(envolopeVersion);
envelope.setOutputSoapObject(request);
envelope.dotNet =true;
HttpTransportSE se = new HttpTransportSE(transUrl);
try {
  //ver11,第一個參數不可為空 。envolopeVersion為ver12第一個參數可以為空白,必須介面支援ver12才行
se.call(null,envelope);
SoapObject response = (SoapObject) envelope.bodyIn;
//response的處理需要根據返回的具體情況,基本都要進行下面一步
SoapObject o = (SoapObject) response.getProperty(0);
Log.e("bbb","MainActivity.java->run():size="+o.getPropertyCount());
 //當前方法返回的結果為一個數組 
for (int i = 0; i<o.getPropertyCount();i++){
Log.e("bbb","MainActivity.java->run(): =="+o.getPropertyAsString(i));
result=result+o.getPropertyAsString(i);
}
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
}
最後調用這方法:
Button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

new Thread(new Runnable() {
@Override
public void run() {
tianqi();
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
resultView.setText(result);
}
});
}
}).start();
}
});

其中的名門空間裡的欄位是可以在
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?WSDL 中找到的。
targetNamespace="http://WebXml.com.cn/" 這個是namespace地址。
s:element name="getSupportCity" 以及各個方法名。
xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" 以及版本號碼12。



Android調用webService

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.