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);中引号里面的属性要与页面上的保持一致。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值