运用android调用webservice实现天气预报查询,实现效果如下图:
首先,实现页面设计,weathersearch.xml中的代码如下:
<?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">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/city" />
<EditText android:id="@+id/city" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button android:id="@+id/search" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/search" />
<TextView android:id="@+id/result" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
在添加一个SoapUtil类,实现方法,代码如下:
package com.example.weathersearch.soap;
import java.io.IOException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
public class SOAPUtil {
public static Object doTransport(final String wsdlUrl,
final String webMethod, String city) {
// WebService的命名空间
String nameSpace = "http://WebXml.com.cn/";
SoapObject soapObject = new SoapObject(nameSpace, webMethod);
soapObject.addProperty("theCityName", city);
System.out.println(city);
System.out.println(wsdlUrl);
System.out.println(webMethod);
// 生成调用Webservice方法的SOAP请求信息
SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
soapSerializationEnvelope.bodyOut = soapObject;
soapSerializationEnvelope.dotNet = true;
soapSerializationEnvelope.setOutputSoapObject(soapObject);
// 创建HttpTransportsSE对象。
HttpTransportSE httpTransportSE = new HttpTransportSE(wsdlUrl);
httpTransportSE.debug = true;
String SOAP_ACTION = "http://WebXml.com.cn/" + webMethod;
System.out.println(SOAP_ACTION);
try {
// 使用call方法调用WebService方法
httpTransportSE.call(SOAP_ACTION, soapSerializationEnvelope);
// 获得WebService方法的返回结果
if (soapSerializationEnvelope.getResponse() != null) {
SoapObject result = (SoapObject) soapSerializationEnvelope
.getResponse();
return result;
}
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
return null;
}
}
再在主要的Activity(WeatherSearch.java)中编写如下代码:
package com.example.weathersearch;
import org.ksoap2.serialization.SoapObject;
import com.example.weathersearch.soap.SOAPUtil;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class WeatherSearchActivity extends Activity {
private EditText city;
private Button search;
private TextView result;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.weathersearch);
city = (EditText) this.findViewById(R.id.city);
search = (Button) this.findViewById(R.id.search);
result = (TextView) this.findViewById(R.id.result);
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String city1 = city.getText().toString();
String wsdlUrl = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx";
String method = "getWeatherbyCityName";
SoapObject result1 = (SoapObject)SOAPUtil.doTransport(wsdlUrl, method, city1);
System.out.println(result1.toString());
result.setText(result1.getProperty(10).toString());
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.weathersearch, menu);
return true;
}
}
注意:实现时首先要在AndroidManifest.xml中配置允许网络接入的语句:<uses-permission android:name="android.permission.INTERNET"/>;在运行时会出现空指针异常,要确保自己连接的网页能够打开,并且在
soapObject.addProperty("theCityName", city);中引号里面的属性要与页面上的保持一致。