小编给大家分享一下android中如何使用Soap协议调用webservice实现手机归属地查询,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
一:Web服务(webservice)是局域网和因特网上能够支持机器与机器之间互操作的软件系统。它有一个用WSDL描述的接口,其它系统可以使用SOAP消息以接口所描述的方式与之交互。SOAP协议是Web服务赖以生存的基础。
Web服务的目标是实现在这样的分布式环境环境中,各个组织内部及各组织之间任意数量的应用程序或应用程序组件能够以与平台无关和语言无关的方式无缝交互。
Web服务是通过统一资源标识URI标识的软件系统,它的共用接口和绑定用XML来定义和描述。软件系统可以通过Internet协议传递基于XML的消息,这样就可以用Web Service 所定义的方式与其交互。
Web服务使我们能够对因特网或网络上的一个对象进行远程调用RPC(Remote Procedure Call)。Web服务使用中性平台标准(HTTP和XML),对客户完全隐藏执行任务的细节,客户只需要知道这个服务的URL或方法调用使用的数据类型,为不同平台提供服务。
SOAP、WSDL、和UDDI是webservice技术体系的核心:
(1)WSDL是Web服务的描述语言,它类似于CORBA的IDL用以描述Web服务的交互消息格式、端口类型以及传输协议的绑定。
(2)Web服务使用UDDI作为目录机制,服务发布者可以将服务信息注册到UDDI,从而方便服务使用者进行服务查找。
(3)SOAP提供一个标准的包装结构用以在多种标准Internet技术上(包括SMTP、HTTP和FTP)传输XML文档。它还定义了用XML传送非XML RPC调用的编码和绑定标准,SOAP为RPC提供了一个简单的结构:文档交换。采用标准传输机制后,异构的客户和服务器能一下子可互操作。
二:SOAP(Simple Object Access Protocol)即简单对象访问协议,它是一个轻型分布式计算协议,允许在一个分散、分布的环境交换结构化的信息。SOAP规范定义了在分布式系统中传送消息的框架和支持远程过程调用和响应的惯例。SOAP消息是以SOAP
Envelope为根元素,内含2个子元素:SOAP Header和SOAP Body。SOAP Body是强制性的,是SOAP消息必须要有的元素,它包含了SOAP消息的主要内容,由最终接收SOAP消息的节点处理。SOAP Body是应用的有效载荷,可以包含应用数据、RPC方法和参数以及SOAP错误。
三:下面是android开发中基于soap协议与服务器交互实现手机归属地查询案例 。
1。运行效果:
不存在此号用户时:
2.布局文件代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/map" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:text="手机号码:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/phone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="@+id/text"
android:layout_marginTop="80dp"
android:layout_toRightOf="@+id/text" >
</EditText>
<Button
android:id="@+id/search"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/phone"
android:text="查询" />
<TextView
android:id="@+id/res"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="250dp"
android:text="查詢結果:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="250dp"
android:layout_toRightOf="@+id/res" />
</RelativeLayout>
3.创建手机归属地查询工具类PhoneUtil.java
public class PhoneUtil {
public static Object searchPlace(String wsdlurl,String method,String phonenumber) {
//指定webservice的命名空间和调用的方法名
String namespace="http://WebXml.com.cn/";
SoapObject soap=new SoapObject(namespace,method);
//添加属性,只要设置参数的顺序一致,调用方法的参数名不一定与服务端的WebService类中的方法参数名一致
soap.addProperty("mobileCode",phonenumber);
soap.addProperty("userID", null);
//通过SoapSerializationEnvelope类的构造方法设置SOAP协议的版本号。
SoapSerializationEnvelope soapEnvelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
//设置需要传出的Soap
soapEnvelope.bodyOut=soap;
soapEnvelope.dotNet=true;
soapEnvelope.setOutputSoapObject(soap);
//创建http传输对象
HttpTransportSE transportSE=new HttpTransportSE(wsdlurl);
//soap操作url
String SOAP_ACTION=namespace+method;
try {
//请求调用WebService方法
transportSE.call(SOAP_ACTION, soapEnvelope);
//使用getResponse获得WebService方法解析xml的返回结果
Object result=soapEnvelope.getResponse();
if(result!=null)
return result;
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
return null;
}
}
4.MainActivity类
public class MainActivity extends Activity {
private EditText phonenum;
private Button search;
private EditText result;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置无标题栏
//setTheme(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
//查找组件
phonenum=(EditText) this.findViewById(R.id.phone);
search=(Button) this.findViewById(R.id.search);
result=(EditText) this.findViewById(R.id.result);
search.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String phone=phonenum.getText().toString();
//web服务端手机归属地url
String wsdlUrl="http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
//调用web提供的方法getMobileCodeInfo得到归属地
Object answer=PhoneUtil.searchPlace(wsdlUrl,"getMobileCodeInfo",phone);
//查询结果显示在结果文本域
result.setText(answer.toString());
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
看完了这篇文章,相信你对“android中如何使用Soap协议调用webservice实现手机归属地查询”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://www.xuebuyuan.com/3253961.html