android、webService 天气预报demo

阅读更多
package com.royal.weather;

import java.io.UnsupportedEncodingException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class WeatherDemoActivity extends Activity {

	private static 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 Button okButton;
//	private SoapObject detail;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		okButton = (Button) findViewById(R.id.ok);
		okButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				showWeather();
			}
		});
	}

	private void showWeather() {
		String city = "北京";
		getWeather(city);
	}

	/**
	 * 根据城市名称获取其天气
	 * 
	 * @param cityName
	 */
	public void getWeather(String cityName) {
		System.out.println("rpc-------------");
		// (1) 指定webservice的命名空间和调用的方法名
		SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);
		System.out.println("rpc " + rpc);
		System.out.println("cityName is " + cityName);

		/**
		 * (2) 设置调用方法的参数值,如果没有参数,可以省略。 要注意的是,addProperty方法的第1个参数虽然表示调用方法的参数名,
		 * 但该参数值并不一定与服务端的WebService类中的方法参数名一致,只要设置参数的顺序一致即可。
		 */
		rpc.addProperty("theCityName", cityName);

		// (3) 生成调用Webservice方法的SOAP请求信息。该信息由SoapSerializationEnvelope对象描述
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);

		envelope.bodyOut = rpc;
		envelope.dotNet = true;
		envelope.setOutputSoapObject(rpc);

		// (4)创建HttpTransportsSE对象。通过AndroidHttpTransport类的构造方法可以指定WebService的WSDL文档的URL

		HttpTransportSE ht = new HttpTransportSE(URL);
		ht.debug = true;

		try {
			// (5)使用call方法调用WebService方法
			// Call方法的第一个参数一般为null,第2个参数就是在第3步创建的SoapSerializationEnvelope对象
			ht.call(SOAP_ACTION, envelope);

			// (6)使用getResponse方法获得WebService方法的返回结果
			SoapObject result = (SoapObject) envelope.getResponse();
			// SoapObject result = (SoapObject) envelope.bodyIn;
			System.out.println("result " + result);

			// detail = (SoapObject) result
			// .getProperty("getWeatherbyCityNameResult");
			// System.out.println("detail " + detail);

			Toast.makeText(WeatherDemoActivity.this, result.toString(),
					Toast.LENGTH_LONG).show();
			parseWeather(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 解析返回的数据
	 * 
	 * @param detail
	 * @throws UnsupportedEncodingException
	 */
	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(WeatherDemoActivity.this, weatherToday,
				Toast.LENGTH_LONG).show();

	}
}

 

  AndroidManifest.xml




    

    
        
            
                

                
            
        
    
    
       

 

别忘了导入KSOAP包.

  • WeatherDemo.rar (186.8 KB)
  • 下载次数: 262
  • ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar (145.4 KB)
  • 下载次数: 174

你可能感兴趣的:(adroid,webservice,天气预报)