android-------使用Android应用调用Web Service
2016-05-17 11:32
567 查看
1、方法步骤
今天研究了下使用Android应用调用Web Service,使用的第三方jar是:ksoap2-android。使用ksoap2-android调用Web Service的操作步骤如下:创建HttpTransportSE对象,该对象用于调用Web Service。
创建SoapSerializationEnvelope对象。
创建SoapObject对象,创建该对象时需要传入所要调用Web Service的命名空间、Web Service的方法名。
如果有参数需要传入Web Service的服务端,需要调用SoapObject对象的addProperty(String name,Object value)方法来设置参数,name指定参数名,value指定参数值。
调用SoapSerializationEnvelope的setOutputSoapObject()方法,或者直接对bodyOut属性赋值,将前两步创建的SoapObject对象设为SoapSerializationEnvelope的传出SOAP消息体。
调用对象的call()方法,并以SoapSerializationEnvelope作为参数调用远程的Web Service。
调用完成后,访问SoapSerializationEnvelope对象的bodyIn属性,该属性返回一个SoapObject对象,该对象代表Web Service返回的消息。解析该SoapObject对象就可获取调用Web Service的返回值。
2、例子(查询号码归属地)
主要的代码:输入号码并单击
queryButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 手机号码(段) final String phoneSec = phoneSecEditText.getText().toString().trim(); // 简单判断用户输入的手机号码(段)是否合法 if ("".equals(phoneSec) || phoneSec.length() < 7) { // 给出错误提示 phoneSecEditText.setError("您输入的手机号码(段)有误!"); phoneSecEditText.requestFocus(); // 将显示查询结果的TextView清空 resultView.setText(""); return; } new Thread(new Runnable() { @Override public void run() { // 查询手机号码(段)信息 getRemoteInfo(phoneSec); } }).start(); } });
获取号码归属地
/** * 手机号段归属地查询 * * @param phoneSec * 手机号段 */ public void getRemoteInfo(String phoneSec) { // 命名空间 String nameSpace = "http://WebXml.com.cn/"; // 调用的方法名称 String methodName = "getMobileCodeInfo"; // EndPoint String endPoint = "http://www.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.VER11); 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(); Message message=handler.obtainMessage(); message.what=0x123; message.obj=result; handler.sendMessage(message); }
消息处理
private Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if(msg.what==0x123){ // 将WebService返回的结果显示在TextView中 resultView.setText((CharSequence) msg.obj); } } };
注意:记得加上访问网络的权限
效果如下图:
例子代码下载:
代码下载地址
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories