Android开发Web Service通信

[size=medium][color=gray] 与HTTP通信方式相比,HTTP不能实现远程方法的调用,Web Service可以实现。Web Service也有服务器端和客户端,一般采用XFire来搭建服务器。客户端Android系统中并没有提供直接与Web Service互相调用的操作类库,一般是采用第三方提供的类库才可以完成,比较常用的是Ksoap2类库。
一个完整的Web Service通信应该有服务器的建设和客户端的建设两个部分,服务器端的建设比较繁琐,可以借助内置Tomcat的MyEclipse8.5来实现。
其实网络上有很多已经建立好的服务器端,在服务器端提供开发好的方法供外界使用,例如:在http://www.webxml.com.cn服务器上对外公开了很多方法,比如天气情况、航班信息、手机归属地等,客户端只需要调用这些方法和相关参数,就可以编程调用这些方法,而不需要自己再去建立服务器。
下面编写了一个实例:
新建主布局文件:[/color][/size]
<?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>
[size=medium][color=gray] 下面是主程序类:[/color][/size]
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();
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值