近期在学习Android,随着移动设备的流行,当软件走上商业化的道路,为了争夺市场,肯定须要支持Android的,所以開始接触了Android,只是仅仅了解皮毛就好,由于我们要做管理者嘛,懂点Android,管理起来easy些。

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

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

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

在Android平台调用WebService须要依赖于第三方类库ksoap2,它是一个SOAP Webserviceclient开发包,主要用于资源受限制的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);
}
}

注意点1nameSpace、methodName 、EndPoint和SOAP
Action
信息,都能够在WSDL中得到。

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

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

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

<string xmlns="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)执行结果

源代码下载

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

Android平台调用Web Service:演示样例的更多相关文章

  1. Android之——多线程下载演示样例

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46883927 一.概述 说到Android中的文件下载.Android API中明 ...

  2. android listview综合使用演示样例_结合数据库操作和listitem单击长按等事件处理

    本演示样例说明: 1.自己定义listview条目样式,自己定义listview显示列数的多少,灵活与数据库中字段绑定. 2.实现对DB的增删改查,而且操作后listview自己主动刷新. 3.响应用 ...

  3. Android平台调用Web Service:线程返回值

    接上文 前文中的遗留问题 对于Java多线程的理解,我曾经只局限于实现Runnable接口或者继承Thread类.然后重写run()方法.最后start()调用就算完事,可是一旦涉及死锁以及对共享资源 ...

  4. Android - 标准VideoView播放演示样例

    标准VideoView播放演示样例 本文地址: http://blog.csdn.net/caroline_wendy 在Android SDK中的ApiDemos内, 提供标准播放视频的代码,使用V ...

  5. Android之——流量管理程序演示样例

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47680811 眼下.市面上有非常多管理手机流量的软件,能够让用户实时获取到自己手机 ...

  6. [Android]RecyclerView的简单演示样例

    去年google的IO上就展示了一个新的ListView.它就是RecyclerView. 下面是官方的说明,我英语能力有限,只是我大概这么理解:RecyclerView会比ListView更具有拓展 ...

  7. Android平台调用Web Service:螺纹的引入

    连接文本 剩下的问题 MainActivity的onCreate方法中假设没有有这段代码: // 强制在UI线程中操作 StrictMode.setThreadPolicy(new StrictMod ...

  8. Android SQLite 简单使用演示样例

    SQLite简单介绍 Google为Andriod的较大的数据处理提供了SQLite,他在数据存储.管理.维护等各方面都相当出色,功能也很的强大. 袖珍型的SQLite能够支持高达2TB大小的数据库, ...

  9. Androidclient与服务端交互之登陆演示样例

    今天了解了一下androidclient与服务端是如何交互的,发现事实上跟web有点类似吧,然后网上找了大神的登陆演示样例.是基于IntentService的 1.后台使用简单的servlet,支持G ...

随机推荐

  1. 利用相关的Aware接口

    Struts 2提供了Aware接口.Aware为"感知"的意思,实现了相关Aware接口的Action能够感知相应的资源.Struts在实例化一个Action实例时,如果发现它实 ...

  2. LayoutInflater作用是将layout的xml布局文件实例化为View类对象。

    获取LayoutInflater的方法有如下三种: LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.L ...

  3. JavaScript 高级程序设计(第3版)笔记——chapter7:函数表达式

    一.函数表达式的语法形式 匿名函数 var functionName = function(arg0, arg1, arg2) { //函数体 } 二.函数表达式没有函数提升 var a = 1; i ...

  4. Jquery Select 下拉框处理

    $("#select").empty();//清空 $("#select").append($("<option/>").val ...

  5. SAP HANA 开发者中心(Developer Center)入门指南

  6. 把Web Form项目转换成MVC项目

    http://umbraco.com/follow-us/blog-archive/2013/7/14/moving-from-webforms-to-mvc.aspx https://codinga ...

  7. 工具篇-TraceView

    --- layout: post title: 工具篇-TraceView  description: 让我们远离卡顿和黑屏 2015-10-09 category: blog --- ## 让我们远 ...

  8. 【转】Ubuntu常用软件合集

    [转]Ubuntu常用软件合集 Ubuntu常用软件合集 我用的使Ubuntu-Kylin14.04,原因呢主要是觉得使本土化的,自带了日历.输入法.优客助手等易于上手的应用.也省的每次安装完原生的系 ...

  9. Ownership qualifiers of Objective-C: In Details

    虽然这里讲的大部分知识以前都看过,但是时不时出现某些点让我如茅塞顿开: 以前经常会忘记一些细节,这篇文章可以更好的理解细节,巩固知识体系. Ownership qualifiers In Object ...

  10. Subsets 【dfs】

    Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...