最近在学习Android,随着移动设备的流行,当软件走上商业化的道路,为了争夺市场,肯定需要支持Android的,所以开始接触了Android,不过只了解皮毛就好,因为我们要做管理者嘛,懂点Android,管理起来容易些。

Android学起来也简单,封装的更好了,一个个的控件,像是又回到了VB的赶脚。

下面将通过一个示例讲解如何在Android平台调用Web Service。我们使用互联网现成的Webservice,供查询手机号码归属地的Web service,它的WSDLhttp://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl

1)新建Android工程,引入上面下载的ksoap2-android类库

在Android平台调用WebService需要依赖于第三方类库ksoap2,它是一个SOAP Webservice客户端开发包,主要用于资源受限制的Java环境如Applets或J2ME应用程序(CLDC/ CDC/MIDP)。

在Android平台中我们并不会直接使用ksoap2,而是使用ksoap2android。KSoap2 Android 是Android平台上一个高效、轻量级的SOAP开发包

2)编写布局文件res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:orientation="vertical"      android:layout_width="fill_parent"      android:layout_height="fill_parent"      android:paddingTop="5dip"      android:paddingLeft="5dip"      android:paddingRight="5dip"      >      <TextView          android:layout_width="fill_parent"           android:layout_height="wrap_content"           android:text="手机号码(段):"      />      <EditText android:id="@+id/phone_sec"          android:layout_width="fill_parent"          android:layout_height="wrap_content"          android:inputType="textPhonetic"          android:singleLine="true"          android:hint="例如:1398547"      />      <Button android:id="@+id/query_btn"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_gravity="right"          android:text="查询"      />      <TextView android:id="@+id/result_text"          android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:layout_gravity="center_horizontal|center_vertical"      />  </LinearLayout>  


3)编写MainActivity类

/**  * Android平台调用WebService(手机号码归属地查询)  *   */  public class MainActivity extends Activity {      private EditText phoneSecEditText;      private TextView resultView;      private Button queryButton;        @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.main);    // 强制在UI线程中操作           StrictMode.setThreadPolicy(newStrictMode.ThreadPolicy.Builder()       .detectDiskReads().detectDiskWrites().detectNetwork()        .penaltyLog().build());                   StrictMode.setVmPolicy(newStrictMode.VmPolicy.Builder()       .detectLeakedSqlLiteObjects().penaltyLog().penaltyDeath()        .build());         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);              }          });      }        /**      * 手机号段归属地查询      *       * @param phoneSec 手机号段      */      public void 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);      }  }  


注意点1nameSpacemethodNameEndPointSOAP Action 信息,都可以在WSDL中得到。

注意点2调用WebService接口方法需要传入的参数时,参数名称要和WSDL中描述的一致。(网上有些资料说在需要传入多个参数时,只要多个参数的顺序与WSDL中参数出现的顺序一致即可,名称并不需要和WSDL中的一致,但实际测试发现,大多数情况下并不可行!

注意点3本例中调用WebService后返回的结果如下所示:

<?xml version="1.0"encoding="utf-8"?>

<stringxmlns="http://WebXml.com.cn/">1398547:贵州贵阳贵州移动黔中游卡</string>

这里明明返回的是xml格式的内容,为什么我们不需要通过解析xml来获取我们需要的内容呢?其实:

//获取返回的数据

SoapObject object = (SoapObject) envelope.bodyIn;

ksoap2能够将返回的xml转换成SoapObject对象,然后我们就可以通过操作对象的方式来获取需要的数据了。

注意点4本例中只返回了一个值,但有些WebService会返回多个值该怎么获取?获取方法与本例完全一样,只是需要注意的是如果是返回多个值,通过第100代码object.getProperty(0);得到的可能仍然是一个SoapObject。不断地调用getProperty()方法;总能得到你想要的全部结果。

4)在AndroidManifest.xml中配置添加访问网络的权限

<?xml version="1.0" encoding="utf-8"?>  <manifest xmlns:android="http://schemas.android.com/apk/res/android"        package="com.liufeng.ws.activity"        android:versionCode="1"        android:versionName="1.0">      <application android:icon="@drawable/icon" android:label="@string/app_name">          <activity android:name=".MainActivity" android:label="@string/app_name">              <intent-filter>                  <action android:name="android.intent.action.MAIN" />                  <category android:name="android.intent.category.LAUNCHER" />              </intent-filter>          </activity>      </application>       <uses-sdk android:minSdkVersion="4" />       <!-- 访问网络的权限 -->      <uses-permission android:name="android.permission.INTERNET" />   </manifest>  

5)运行结果

Android平台调用Web Service:示例_第1张图片


源码下载

http://download.csdn.net/detail/tcl_6666/7365311

更多相关文章

  1. Android、iPhone和Java三个平台一致的加密工具
  2. Android 版本 相关参数修改(SpreadTrum)
  3. 多平台部署
  4. Android访问WCF服务(使用json实现参数传递)
  5. Android平台上优秀的开源项目
  6. 在android平台上编译libpcap-0.9.8 和 jnetpcap-1.2.rc1
  7. Android中Activity之间访问互传参数
  8. Android之实现手机号码拦截

随机推荐

  1. Android定时器AlarmManager和Timer的区别
  2. Dialog使用findViewById 报空指针异常
  3. 浅析Android中build.gradle的实用技巧
  4. ImageView宽度填满屏幕,高度自适应
  5. Android(安卓)中文API (68) ―― Bluetooth
  6. Android入门开发之SD卡读写操作
  7. Android中在代码中设置控件的宽和高
  8. HttpURLConnection和HttpClient区别
  9. getLayoutInflater().inflate
  10. 自学第五十九天