標籤:
以查詢手機號碼歸屬地的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 介面