android調用webservice實現天氣預報查詢

運用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);中引號裏面的屬性要與頁面上的保持一致。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章