码迷,mamicode.com
首页 > 移动开发 > 详细

Android开发之WebServices

时间:2015-02-16 01:40:35      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

一:WebServices简介

关于webservices的介绍在W3C上讲的很详细。webservice就是一个网络应用程序,基本的WebService平台是XML+HTTP。

  • HTTP 协议是最常用的因特网协议。
  • XML 提供了一种可用于不同的平台和编程语言之间的语言。
Web services 平台的元素:
  • SOAP (简易对象访问协议) :基于 XML,独立于语言,通过 HTTP 在应用程序间交换信息。
  • UDDI (通用描述、发现及整合):由 WSDL 描述的网络服务接口目录,通过它,企业可注册并搜索 Web services。
  • WSDL (Web services 描述语言):基于 XML 的用来描述 Web services 以及如何访问它们的一种语言。

webService的使用

通过表单和 HTTP POST,将 web service 置于站点上提供服务。通过XML 在应用程序间发送消息。

通过使用WebService,我们能够像调用本地方法一样去调用远程服务器上的方法。并不需要关心服务器是用什么写的 ,基于什么平台。

二:android平台下的WebService调用

在android中调用WebService需要导入第三方jar包,ksoap2-android,右间工程“Build Path”-“Config Build Path...”,将该jar包复选框选中,确定。

三:应用

这个应用主要是利用http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx的号码归属地WebService来实现归属地的查询:

<?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>

CODE:

这里面对于简单的EditText输入校验就不做了,另外这里需要搞清楚nameSpace ,methodName,asmx,Action变量,在http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?WSDL说明文档中,都写的很清除了asmx就是WSDL文档去掉后面的参数,实践过程中,发现带上也可以请求到数据。Action就是   命名空间  加上  方法名。

   /** 
     * 手机号段归属地查询 
     *  
     * @param phoneSec 手机号段 
     */  
    public void getRemoteInfo(String phoneSec) {  
        // 命名空间  
        String nameSpace = "http://WebXml.com.cn/";  
        // 调用的方法名称  
        String methodName = "getMobileCodeInfo";  
        // asmx  
        String asmx = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";  
        // SOAP Action  
        String soapAction = "http://WebXml.com.cn/getMobileCodeInfo";  
  
        // 指定WebService的命名空间和调用的方法名  
        SoapObject sobj = new SoapObject(nameSpace, methodName);  
  
        // 设置需调用WebService接口需要传入的两个参数mobileCode、userId  
        sobj.addProperty("mobileCode", phoneSec);  
        sobj.addProperty("userId", "");  
  
        // 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本  
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);  
  
        envelope.bodyOut = sobj;  
        // 设置是否调用的是dotNet开发的WebService  
        envelope.dotNet = true;  
        // 等价于envelope.bodyOut = rpc;  
        envelope.setOutputSoapObject(sobj);  
  
        HttpTransportSE transport = new HttpTransportSE(asmx);  
        try {  
            // 调用WebService  
            transport.call(soapAction, envelope);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
  
        // 获取返回的数据  
        SoapObject object = (SoapObject) envelope.bodyIn/*.getResponse()*/;  
        // 获取返回的结果  
        String result = object.getProperty("getMobileCodeInfoResult"/*0*/).toString();  
  
        // 将WebService返回的结果显示在TextView中  
        resultView.setText(result);  
    }

看到上面获取对象和获取结果可以有两种方式。都是saop中提供的方法。

如果在获取到的放回数据 有多个值 ,可以通过result.getPropertyCount() 来获取所的结果的个数

在某些时候,可能会报一些莫名异常,可以尝试修改sopa版本 ,或者将action设为null(有些不需要action)

权限:

最后一件事 :不要忘记加权限

<uses-permission android:name="android.permission.INTERNET" />

Android开发之WebServices

标签:

原文地址:http://www.cnblogs.com/BoBoMEe/p/4293638.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!