Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> Android開發中webService接口調用示例

Android開發中webService接口調用示例

編輯:Android開發實例

  代碼示例:

  package com.study.ws;
  import java.io.IOException;
  import org.ksoap2.SoapEnvelope;
  import org.ksoap2.SoapFault;
  import org.ksoap2.serialization.SoapObject;
  import org.ksoap2.serialization.SoapSerializationEnvelope;
  import org.ksoap2.transport.AndroidHttpTransport;
  import org.ksoap2.transport.HttpTransportSE;
  import org.xmlpull.v1.XmlPullParserException;
  import android.app.Activity;
  import android.os.Bundle;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.webkit.WebSettings;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.TextView;
  public class WSClient extends Activity {
   private Button search;
   private EditText phone;
   private TextView  result;
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          search=(Button)findViewById(R.id.search);
          phone=(EditText)findViewById(R.id.phone);
          result=(TextView)findViewById(R.id.result);
          search.setOnClickListener(new OnClickListener() {
    
     @Override
     public void onClick(View v) {
      String strPhone=phone.getText().toString().trim();
      if(!(strPhone.length()==0||strPhone.equals("")))
      {
        String str = "";
      try {
       str = getPhoneInfo(strPhone);
      } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      } catch (XmlPullParserException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
        result.setText(str);
      }
     }
    });
         
      }
      public String getPhoneInfo(String phoneName) throws IOException, XmlPullParserException
      {
       //返回的查詢結果
       String result = null;
       //調用webservice接口的名稱空間
       String nameSpace="http://WebXml.com.cn/";
       //調用的方法名
       String methodName="getMobileCodeInfo";
       //將方法名和名稱空間綁定在一起
     String SOAP_ACTION = nameSpace + methodName;
    //獲得返回請求對象
    SoapObject request = new SoapObject(nameSpace, methodName);
    //設置需要返回請求對象的參數
    request.addProperty("mobileCode", phoneName);
    request.addProperty("userId", "");
    //設置soap的版本
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
      SoapEnvelope.VER11);
          //設置是否調用的是dotNet開發的
    envelope.dotNet = true;
    //
    envelope.bodyOut = request;
    AndroidHttpTransport hts = new AndroidHttpTransport

  ("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx");
    // web service請求
    hts.call(SOAP_ACTION, envelope);
    // 得到返回結果
    Object o = envelope.getResponse();
    result = o.toString();
   
    return result;
  
  }
   
  }

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved