Android調用webservice 介面

來源:互聯網
上載者:User

標籤:

以查詢手機號碼歸屬地的Web service為例,它的wsdl為

http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl

1 在Android項目中匯入??Ksoap2-android jar第三方jar包??

2 Activity代碼

public class SecondActivity extends Activity {private EditText phoneSecEditText;      private TextView resultView;      private Button queryButton;        @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_second);            phoneSecEditText = (EditText) findViewById(R.id.phone_sec);          resultView = (TextView) findViewById(R.id.result_text);          queryButton = (Button) findViewById(R.id.query_btn);            queryButton.setOnClickListener(new OnClickListener() {              @Override              public void onClick(View v) {                  // 手機號碼(段)                  String phoneSec = phoneSecEditText.getText().toString().trim();                  // 簡單判斷使用者輸入的手機號碼(段)是否合法                  if ("".equals(phoneSec) || phoneSec.length() < 7) {                      // 給出錯誤提示                      phoneSecEditText.setError("您輸入的手機號碼(段)有誤!");                      phoneSecEditText.requestFocus();                      // 將顯示查詢結果的TextView清空                      resultView.setText("");                      return;                  }                  // 查詢手機號碼(段)資訊  //                getRemoteInfo(phoneSec);                  new Mys().execute(phoneSec) ;            }          });      }        /**      * 手機號段歸屬地查詢      *       * @param phoneSec 手機號段      */      public String getRemoteInfo(String phoneSec) {          // 命名空間          String nameSpace = "http://WebXml.com.cn/";          // 調用的方法名稱          String methodName = "getMobileCodeInfo";          // EndPoint          String endPoint = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";          // SOAP Action          String soapAction = "http://WebXml.com.cn/getMobileCodeInfo";            // 指定WebService的命名空間和調用的方法名          SoapObject rpc = new SoapObject(nameSpace, methodName);            // 設定需調用WebService介面需要傳入的兩個參數mobileCode、userId          rpc.addProperty("mobileCode", phoneSec);          rpc.addProperty("userId", "");            // 產生調用WebService方法的SOAP請求資訊,並指定SOAP的版本          SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);            envelope.bodyOut = rpc;          // 設定是否調用的是dotNet開發的WebService          envelope.dotNet = true;          // 等價於envelope.bodyOut = rpc;          envelope.setOutputSoapObject(rpc);            HttpTransportSE transport = new HttpTransportSE(endPoint);          try {              // 調用WebService              transport.call(soapAction, envelope);          } catch (Exception e) {              e.printStackTrace();          }            // 擷取返回的資料          SoapObject object = (SoapObject) envelope.bodyIn;          // 擷取返回的結果          String result = object.getProperty(0).toString();            // 將WebService返回的結果顯示在TextView中  //        resultView.setText(result);          return result ;    }         class Mys extends AsyncTask<String, Void, String>{@Overrideprotected String doInBackground(String... params) {return getRemoteInfo(params[0]) ;}@Overrideprotected void onPostExecute(String result) {resultView.setText(result);  super.onPostExecute(result);}      }}

其中,

// 命名空間          String nameSpace = "http://WebXml.com.cn/";          // 調用的方法名稱          String methodName = "getMobileCodeInfo";          // EndPoint          String endPoint = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";          // SOAP Action          String soapAction = "http://WebXml.com.cn/getMobileCodeInfo";

這些值分別為

    命名空間、調用的方法名稱、EndPoint和SOAP Action

    EndPoint 通常是將WSDL地址末尾的"?wsdl"去除後剩餘的部分

    SOAP Action通常為命名空間 + 調用的方法名稱。    


參考:http://blog.csdn.net/lyq8479/article/details/6428288/


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.