最近无聊学习了一下android作为客户端调用WebSevice的例子,参考上面那篇博客。。。
其实对于简单的应用,这是很容易实现的。
由于android没有支持WebService的库,所以我们得一下个 。
KSOAP2 地址:http://code.google.com/p/ksoap2-android/
下载完成后,将包导入项目。
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
准备工作完成,下面我们来学习一下怎么具体调用。。
1、指定 WebService 的命名空间和调用方法
import org.ksoap2.serialization.SoapObject; private static final String NAMESPACE = "http://WebXml.com.cn/"; private static final String METHOD_NAME = "getWeatherbyCityName"; SoapObject sop= new SoapObject(NAMESPACE, METHOD_NAME);
SoapObject类的第一个参数表示WebService的命名空间,可以从WSDL文档中找到WebService的命名空间。
第二个参数表示要调用的WebService方法名。
2、设置调用方法的参数值,如果没有参数,可以省略,设置方法的参数值的代码如下:
sop.addProperty("theCityName", "北京");
要注意的是,addProperty方法的第1个参数虽然表示调用方法的参数名,但该参数值并不一定与服务端的WebService类中的方法参数名一致,只要设置参数的顺序一致即可。
3、生成调用Webservice方法的SOAP请求信息。
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.bodyOut = rpc; envelope.dotNet = true; envelope.setOutputSoapObject(rpc);
创建SoapSerializationEnvelope对象时需要通过SoapSerializationEnvelope类的构造方法设置SOAP协议的版本号。
该版本号需要根据服务端WebService的版本号设置。
在创建SoapSerializationEnvelope对象后,不要忘了设置SOAPSoapSerializationEnvelope类的bodyOut属性,
该属性的值就是在第一步创建的SoapObject对象。
4、创建HttpTransportsSE对象。
这里不要使用 AndroidHttpTransport ht = new AndroidHttpTransport(URL); 这是一个要过期的类
private static String URL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx"; HttpTransportSE ht = new HttpTransportSE(URL);
ht.debug = true;
5、使用call方法调用WebService方法
private static String SOAP_ACTION = "http://WebXml.com.cn/getWeatherbyCityName"; ht.call(SOAP_ACTION, envelope);
网上有人说这里的call的第一个参数为null,但是经过我的测试,null是不行的。
第2个参数就是在第3步创建的SoapSerializationEnvelope对象。
6、获得WebService方法的返回结果
有两种方法:
1、使用getResponse方法获得返回数据。
private SoapObject detail;
detail =(SoapObject) envelope.getResponse();
2、使用 bodyIn 及 getProperty。
private SoapObject detail; SoapObject result = (SoapObject)envelope.bodyIn; detail = (SoapObject) result.getProperty("getWeatherbyCityNameResult");
7、 这时候执行会出错,提示没有权限访问网络
需要修改 AndroidManifest.xml 文件,赋予相应权限
简单来说就是增加下面这行配置:<uses-permission android:name="android.permission.INTERNET"></uses-permission>
完整的 AndroidManifest.xml 文件 如下:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="ghj1976.MyWeather" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.INTERNET"></uses-permission> </manifest>
完整代码如下:
import java.io.UnsupportedEncodingException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
//import org.ksoap2.transport.AndroidHttpTransport;
import org.ksoap2.transport.HttpTransportSE;
public class MainActivity extends Activity {
private Button okButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
okButton = (Button) this.findViewById(R.id.btn_Search);
okButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
String city = "北京";
getWeather(city);
}
});
}
private static final String NAMESPACE = "http://WebXml.com.cn/";
// WebService地址
private static String URL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx";
private static final String METHOD_NAME = "getWeatherbyCityName";
private static String SOAP_ACTION = "http://WebXml.com.cn/getWeatherbyCityName";
private String weatherToday;
private SoapObject detail;
public void getWeather(String cityName) {
try {
System.out.println("rpc------");
SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);
System.out.println("rpc" + rpc);
System.out.println("cityName is " + cityName);
rpc.addProperty("theCityName", cityName);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = rpc;
envelope.dotNet = true;
envelope.setOutputSoapObject(rpc);
HttpTransportSE ht = new HttpTransportSE(URL);
//AndroidHttpTransport ht = new AndroidHttpTransport(URL);
ht.debug = true;
ht.call(SOAP_ACTION, envelope);
//ht.call(null, envelope);
//SoapObject result = (SoapObject)envelope.bodyIn;
//detail = (SoapObject) result.getProperty("getWeatherbyCityNameResult");
detail =(SoapObject) envelope.getResponse();
//System.out.println("result" + result);
System.out.println("detail" + detail);
Toast.makeText(this, detail.toString(), Toast.LENGTH_LONG).show();
parseWeather(detail);
return;
} catch (Exception e) {
e.printStackTrace();
}
}
private void parseWeather(SoapObject detail)
throws UnsupportedEncodingException {
String date = detail.getProperty(6).toString();
weatherToday = "今天:" + date.split(" ")[0];
weatherToday = weatherToday + "\n天气:" + date.split(" ")[1];
weatherToday = weatherToday + "\n气温:"
+ detail.getProperty(5).toString();
weatherToday = weatherToday + "\n风力:"
+ detail.getProperty(7).toString() + "\n";
System.out.println("weatherToday is " + weatherToday);
Toast.makeText(this, weatherToday, Toast.LENGTH_LONG).show();
}
}
这时候你点击button后,Toast会提示北京的天气信息。。。注意要联网哦。。