当前位置:   article > 正文

android之使用Soap协议调用webservice实现手机归属地查询_soap userid

soap userid

 一: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 HeaderSOAP Body。SOAP Body是强制性的,是SOAP消息必须要有的元素,它包含了SOAP消息的主要内容,由最终接收SOAP消息的节点处理。SOAP Body是应用的有效载荷,可以包含应用数据、RPC方法和参数以及SOAP错误。

三:下面是android开发中基于soap协议与服务器交互实现手机归属地查询案例  。

1。运行效果:

    

不存在此号用户时:

 

2.布局文件代码:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="@drawable/map" >
  6. <TextView
  7. android:id="@+id/text"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:layout_marginTop="80dp"
  11. android:text="手机号码:"
  12. android:textAppearance="?android:attr/textAppearanceLarge" />
  13. <EditText
  14. android:id="@+id/phone"
  15. android:layout_width="fill_parent"
  16. android:layout_height="wrap_content"
  17. android:layout_alignParentRight="@+id/text"
  18. android:layout_marginTop="80dp"
  19. android:layout_toRightOf="@+id/text" >
  20. </EditText>
  21. <Button
  22. android:id="@+id/search"
  23. android:layout_width="60dp"
  24. android:layout_height="wrap_content"
  25. android:layout_alignParentRight="true"
  26. android:layout_below="@+id/phone"
  27. android:text="查询" />
  28. <TextView
  29. android:id="@+id/res"
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content"
  32. android:layout_marginTop="250dp"
  33. android:text="查詢結果:"
  34. android:textAppearance="?android:attr/textAppearanceLarge" />
  35. <EditText
  36. android:id="@+id/result"
  37. android:layout_width="fill_parent"
  38. android:layout_height="wrap_content"
  39. android:layout_marginTop="250dp"
  40. android:layout_toRightOf="@+id/res" />
  41. </RelativeLayout>


3.创建手机归属地查询工具类PhoneUtil.java

  1. public class PhoneUtil {
  2. public static Object searchPlace(String wsdlurl,String method,String phonenumber) {
  3. //指定webservice的命名空间和调用的方法名
  4. String namespace="http://WebXml.com.cn/";
  5. SoapObject soap=new SoapObject(namespace,method);
  6. //添加属性,只要设置参数的顺序一致,调用方法的参数名不一定与服务端的WebService类中的方法参数名一致
  7. soap.addProperty("mobileCode",phonenumber);
  8. soap.addProperty("userID", null);
  9. //通过SoapSerializationEnvelope类的构造方法设置SOAP协议的版本号。
  10. SoapSerializationEnvelope soapEnvelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
  11. //设置需要传出的Soap
  12. soapEnvelope.bodyOut=soap;
  13. soapEnvelope.dotNet=true;
  14. soapEnvelope.setOutputSoapObject(soap);
  15. //创建http传输对象
  16. HttpTransportSE transportSE=new HttpTransportSE(wsdlurl);
  17. //soap操作url
  18. String SOAP_ACTION=namespace+method;
  19. try {
  20. //请求调用WebService方法
  21. transportSE.call(SOAP_ACTION, soapEnvelope);
  22. //使用getResponse获得WebService方法解析xml的返回结果
  23. Object result=soapEnvelope.getResponse();
  24. if(result!=null)
  25. return result;
  26. } catch (IOException e) {
  27. e.printStackTrace();
  28. } catch (XmlPullParserException e) {
  29. e.printStackTrace();
  30. }
  31. return null;
  32. }
  33. }

4.MainActivity类

  1. public class MainActivity extends Activity {
  2. private EditText phonenum;
  3. private Button search;
  4. private EditText result;
  5. @Override
  6. public void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. //设置无标题栏
  9. //setTheme(Window.FEATURE_NO_TITLE);
  10. setContentView(R.layout.activity_main);
  11. //查找组件
  12. phonenum=(EditText) this.findViewById(R.id.phone);
  13. search=(Button) this.findViewById(R.id.search);
  14. result=(EditText) this.findViewById(R.id.result);
  15. search.setOnClickListener(new OnClickListener() {
  16. public void onClick(View v) {
  17. String phone=phonenum.getText().toString();
  18. //web服务端手机归属地url
  19. String wsdlUrl="http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
  20. //调用web提供的方法getMobileCodeInfo得到归属地
  21. Object answer=PhoneUtil.searchPlace(wsdlUrl,"getMobileCodeInfo",phone);
  22. //查询结果显示在结果文本域
  23. result.setText(answer.toString());
  24. }
  25. });
  26. }
  27. @Override
  28. public boolean onCreateOptionsMenu(Menu menu) {
  29. getMenuInflater().inflate(R.menu.activity_main, menu);
  30. return true;
  31. }
  32. }


 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/41226
推荐阅读
相关标签
  

闽ICP备14008679号