Android开发Web Service通信

   与HTTP通信方式相比,HTTP不能实现远程方法的调用,Web Service可以实现。Web Service也有服务器端和客户端,一般采用XFire来搭建服务器。客户端Android系统中并没有提供直接与Web Service互相调用的操作类库,一般是采用第三方提供的类库才可以完成,比较常用的是Ksoap2类库。
   一个完整的Web Service通信应该有服务器的建设和客户端的建设两个部分,服务器端的建设比较繁琐,可以借助内置Tomcat的MyEclipse8.5来实现。
   其实网络上有很多已经建立好的服务器端,在服务器端提供开发好的方法供外界使用,例如:在http://www.webxml.com.cn服务器上对外公开了很多方法,比如天气情况、航班信息、手机归属地等,客户端只需要调用这些方法和相关参数,就可以编程调用这些方法,而不需要自己再去建立服务器。
   下面编写了一个实例:
   新建主布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/et"
        android:hint="请输入城市"
        android:selectAllOnFocus="true"/>

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/search_but"
        android:text="获取天气信息"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text"
        android:textSize="24sp"/>

</LinearLayout>
   下面是主程序类:
package xiao.fuyan.testapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

/**
 * Created by xiao on 2017/2/3.
 */
public class WebServiceActivity extends Activity {
    private Button search_but;
    private TextView textView;
    private EditText et;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webservice_xml);

        et = (EditText) findViewById(R.id.et);
        search_but = (Button) findViewById(R.id.search_but);
        search_but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String city = et.getText().toString();
                getWeather(city, " ");
            }
        });
    }

    //定义命名空间
    private static final String NAMESPACE = "http://WebXml.com.cn/";
    //定义请求WSDL文档的URL
    private static final String URL = "http://www.webxml.com.cn/WebServices/WeatherWS.asmx";
    //定义调用方法
    private static final String METHOD_NAME = "getWeather";
    //定义命名空间+调用方法名
    private static final String SOAP_ACTION = "http://WebXml.com.cn/getWeather";

    private SoapObject detail;

    public void getWeather(String city, String id){
        try {
            textView = (TextView) findViewById(R.id.text);
            //实例化SoapObject对象
            SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);
            //添加参数
            rpc.addProperty("thecityname", city);
            rpc.addProperty("id", id);

            //实例化SoapSerializationEnvelope对象
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            //封装输入的SoapObject对象rpc
            envelope.bodyOut = rpc;
            //要访问的服务器是.net服务器,否则设置为false
            envelope.dotNet = true;
            //设置要输出的SoapObject对象
            envelope.setOutputSoapObject(rpc);
            //指定WSDL地址
            HttpTransportSE ht = new HttpTransportSE(URL);
            //使用调试
            ht.debug = true;

            //调用web service
            ht.call(SOAP_ACTION, envelope);
            //获取返回信息
            detail = (SoapObject) envelope.getResponse();
            textView.setText(detail.toString());
            return;
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

猜你喜欢

转载自xiaofuyan.iteye.com/blog/2355221