设为首页
优惠IDC
收藏本站
六狼博客
六狼论坛
开启辅助访问
切换到窄版
用户名
Email
自动登录
找回密码
密码
登录
立即注册
只需一步,快速开始
只需一步,快速开始
快捷导航
门户
首页
BBS
云计算
大数据
手机
移动开发android,ios,windows phone,windows mobile
编程
编程技术java,php,python,delphi,ruby,c,c++
前端
WEB前端htmlcss,javascript,jquery,html5
数据库
数据库开发Access,mysql,oracle,sql server,MongoDB
系统
操作系统windows,linux,unix,os,RedHat,tomcat
架构
项目管理
软件设计,架构设计,面向对象,设计模式,项目管理
企业
服务
运维实战
神马
搜索
搜索
热搜:
php
java
python
ruby
hadoop
sphinx
solr
ios
android
windows
centos
本版
帖子
用户
六狼论坛
»
首页
›
移动开发
›
Android开发
›
android调用webservice实现手机归属查询
返回列表
查看:
61
|
回复:
0
android调用webservice实现手机归属查询
[复制链接]
jackjobs
jackjobs
当前离线
积分
172
窥视卡
雷达卡
升级
81.33%
当前用户组为
秀才
当前积分为
172
, 升到下一级还需要 28 点。
52
主题
52
主题
52
主题
秀才
秀才, 积分 172, 距离下一级还需 28 积分
秀才, 积分 172, 距离下一级还需 28 积分
积分
172
发消息
楼主
|
发表于 2013-1-30 04:07:33
|
显示全部楼层
|
阅读模式
android中的webservice调用方式与java中差异较大,经过查找些资料和例子,自己这边儿就做了个总结,写了个例子,谈不上原创(惭愧ing...),不过这方面的例子确实都大体相似,因为大家能写成博文的,发布成例子的,调用的接口大抵是网上公开的接口,如天气啊,手机号码段啊之类的,公司内部的接口,肯定是不能外透的,但是调用的方式确实一样,所以记录下来,方便大家研究,或者自己今后用到。
老习惯,先上效果图:
图一
图二
再上项目结构图:
第一步,加入调用webservice的jar包,ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar,下面会提供附件下载。
第二步,编辑activity_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>
第三步,编辑MainActivity.java,代码如下:
package com.android.ws;import org.ksoap2.SoapEnvelope;import org.ksoap2.serialization.SoapObject;import org.ksoap2.serialization.SoapSerializationEnvelope;import org.ksoap2.transport.HttpTransportSE;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;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.activity_main); 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.VER11); envelope.bodyOut = rpc; // 设置是否调用的是dotNet开发的WebService envelope.dotNet = true; // 等价于envelope.bodyOut = rpc; envelope.setOutputSoapObject(rpc); HttpTransportSE transport = new HttpTransportSE(endPoint); System.out.println("rpc:"+rpc); System.out.println("enevlope:"+envelope); 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); } } 第四步,在Mainfest中提供网络权限
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.ws" android:versionCode="1" android:versionName="1.0" > <!-- 添加网络权限 --> <uses-permission android:name="android.permission.INTERNET" /> <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
最后,运行起来,就可看到上图的效果了,下面提供jar和项目的附件下载。
回复
使用道具
举报
提升卡
置顶卡
沉默卡
喧嚣卡
变色卡
千斤顶
显身卡
返回列表
高级模式
B
Color
Image
Link
Quote
Code
Smilies
您需要登录后才可以回帖
登录
|
立即注册
本版积分规则
发表回复
回帖后跳转到最后一页
Copyright © 2008-2020
六狼论坛
(http://it.6wolf.com) 版权所有 All Rights Reserved.
Powered by
Discuz!
X3.4
京ICP备14020293号-2
本网站内容均收集于互联网,如有问题请联系
QQ:389897944
予以删除
快速回复
返回顶部
返回列表