android webservice获取城市天气情况

 

 

Weather.java

package com.example;

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.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class Weather extends Activity {



	 private Button search_but;
	 private TextView textview1;
	 private EditText et;
	 private Button city1_1;
	 private Button city1_2;
	 private Button city1_3;
	 private Button city1_4;
	 private ImageView image1;
	 private ImageView image2;
	 /** Called when the activity is first created. */
	 @Override
	 public void onCreate(Bundle savedInstanceState) {
	  super.onCreate(savedInstanceState);
	  setContentView(R.layout.main);

	 et=(EditText)findViewById(R.id.et);
	 image1=(ImageView)findViewById(R.id.image1);
	 image2=(ImageView)findViewById(R.id.image2);
	 search_but=(Button)findViewById(R.id.search_but);
	 city1_1=(Button)findViewById(R.id.city1_1);
	 city1_1.setOnClickListener(listener1);
	 city1_2=(Button)findViewById(R.id.city1_2);
	 city1_2.setOnClickListener(listener2);
	 city1_3=(Button)findViewById(R.id.city1_3);
	 city1_3.setOnClickListener(listener3);
	 city1_4=(Button)findViewById(R.id.city1_4);
	 city1_4.setOnClickListener(listener4);
	 search_but.setOnClickListener(listener);
	 }
	 private OnClickListener listener=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			String city=et.getText().toString();
			getWeather(city);
		}
	}; 
	private OnClickListener listener1=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			et.setText("北京");
		}
	};
	private OnClickListener listener2=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			et.setText("上海");
		}
	};
	private OnClickListener listener3=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			et.setText("深圳");
		}
	};
	private OnClickListener listener4=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			et.setText("北京");
		}
	};
	

	 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;

	 private String weatherNow;
	 
	 private String weatherWillBe;
	 
	 private void setIcon(String weather, ImageView imageview) {
			if(weather.equalsIgnoreCase("nothing.gif"))
				imageview.setBackgroundResource(R.drawable.a_nothing);
			if(weather.equalsIgnoreCase("0.gif"))
				imageview.setBackgroundResource(R.drawable.a_0);
			if(weather.equalsIgnoreCase("1.gif"))
				imageview.setBackgroundResource(R.drawable.a_1);
			if(weather.equalsIgnoreCase("2.gif"))
				imageview.setBackgroundResource(R.drawable.a_2);
			if(weather.equalsIgnoreCase("3.gif"))
				imageview.setBackgroundResource(R.drawable.a_3);		
			if(weather.equalsIgnoreCase("4.gif"))
				imageview.setBackgroundResource(R.drawable.a_4);
			if(weather.equalsIgnoreCase("5.gif"))
				imageview.setBackgroundResource(R.drawable.a_5);
			if(weather.equalsIgnoreCase("6.gif"))
				imageview.setBackgroundResource(R.drawable.a_6);
			if(weather.equalsIgnoreCase("7.gif"))
				imageview.setBackgroundResource(R.drawable.a_7);
			if(weather.equalsIgnoreCase("8.gif"))
				imageview.setBackgroundResource(R.drawable.a_8);
			if(weather.equalsIgnoreCase("9.gif"))
				imageview.setBackgroundResource(R.drawable.a_9);
			if(weather.equalsIgnoreCase("10.gif"))
				imageview.setBackgroundResource(R.drawable.a_10);			
			if(weather.equalsIgnoreCase("11.gif"))
				imageview.setBackgroundResource(R.drawable.a_11);
			if(weather.equalsIgnoreCase("12.gif"))
				imageview.setBackgroundResource(R.drawable.a_12);
			if(weather.equalsIgnoreCase("13.gif"))
				imageview.setBackgroundResource(R.drawable.a_13);
			if(weather.equalsIgnoreCase("14.gif"))
				imageview.setBackgroundResource(R.drawable.a_14);
			if(weather.equalsIgnoreCase("15.gif"))
				imageview.setBackgroundResource(R.drawable.a_15);
			if(weather.equalsIgnoreCase("16.gif"))
				imageview.setBackgroundResource(R.drawable.a_16);
			if(weather.equalsIgnoreCase("17.gif"))
				imageview.setBackgroundResource(R.drawable.a_17);
			if(weather.equalsIgnoreCase("18.gif"))
				imageview.setBackgroundResource(R.drawable.a_18);
			if(weather.equalsIgnoreCase("19.gif"))
				imageview.setBackgroundResource(R.drawable.a_19);
			if(weather.equalsIgnoreCase("20.gif"))
				imageview.setBackgroundResource(R.drawable.a_20);
			if(weather.equalsIgnoreCase("21.gif"))
				imageview.setBackgroundResource(R.drawable.a_21);
			if(weather.equalsIgnoreCase("22.gif"))
				imageview.setBackgroundResource(R.drawable.a_22);
			if(weather.equalsIgnoreCase("23.gif"))
				imageview.setBackgroundResource(R.drawable.a_23);
			if(weather.equalsIgnoreCase("24.gif"))
				imageview.setBackgroundResource(R.drawable.a_24);
			if(weather.equalsIgnoreCase("25.gif"))
				imageview.setBackgroundResource(R.drawable.a_25);
			if(weather.equalsIgnoreCase("26.gif"))
				imageview.setBackgroundResource(R.drawable.a_26);
			if(weather.equalsIgnoreCase("27.gif"))
				imageview.setBackgroundResource(R.drawable.a_27);
			if(weather.equalsIgnoreCase("28.gif"))
				imageview.setBackgroundResource(R.drawable.a_28);
			if(weather.equalsIgnoreCase("29.gif"))
				imageview.setBackgroundResource(R.drawable.a_29);
			if(weather.equalsIgnoreCase("30.gif"))
				imageview.setBackgroundResource(R.drawable.a_30);			
			if(weather.equalsIgnoreCase("31.gif"))
				imageview.setBackgroundResource(R.drawable.a_31);
		}
	 public void getWeather(String cityName) {
	  try {
	   textview1 = (TextView) this.findViewById(R.id.TextView01);
	   SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);
	   rpc.addProperty("theCityName", cityName);

	   SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
	     SoapEnvelope.VER11);
	   envelope.bodyOut = rpc;
	   envelope.dotNet = true;
	   envelope.setOutputSoapObject(rpc);
	   HttpTransportSE ht = new HttpTransportSE(URL);

	   ht.debug = true;

	   ht.call(SOAP_ACTION, envelope);

	   detail = (SoapObject) envelope.getResponse();
	   parseWeather(detail);

	   return;
	  } catch (Exception e) {
	   e.printStackTrace();
	  }
	 }


	 
	 private void parseWeather(SoapObject detail)
	  throws UnsupportedEncodingException {
	  textview1 = (TextView) this.findViewById(R.id.TextView01);

	  String date = detail.getProperty(6).toString();
	  
	  
	  weatherToday = "\n天气:" + date.split(" ")[1];
	  weatherToday = weatherToday + "\n气温:"
	    + detail.getProperty(5).toString();
	  weatherToday = weatherToday + "\n风力:"
	    + detail.getProperty(7).toString() + "\n";
	  weatherNow = detail.getProperty(8).toString();
	  weatherWillBe = detail.getProperty(9).toString();

	  textview1.setText(et.getText() + weatherToday);
	  setIcon(weatherNow, image1);
	  setIcon(weatherWillBe, image2);

	 }
	}




 

main.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" 
	android:background="@drawable/bg">
	<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
		android:layout_width="fill_parent" 
		android:layout_height="wrap_content"
		android:orientation="horizontal">

		<EditText android:id="@+id/et" 
		android:layout_width="237dp"
			android:layout_height="50dp" 
			android:background="@drawable/shape"
			android:drawableLeft="@drawable/title" 
			android:hint="北京/beijing/bj"
			android:padding="10px" 
			android:singleLine="true" 
			android:textColor="#FFFAFA" />

		<Button android:id="@+id/search_but" 
		android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			 android:background="#00ff00ff"
			android:text="Search" 
			android:textColor="#696969" 
			android:textStyle="italic" />

	</LinearLayout>
	<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
		android:layout_width="fill_parent" 
		android:layout_height="wrap_content"
		android:orientation="horizontal">
		<TableRow>
			<Button
                android:id="@+id/city1_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#00000000"
                android:text="北京"
                android:layout_weight="0.25"
                android:textSize="20px"
                android:textColor="#FFFAFA" />

            <Button
                android:id="@+id/city1_2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#00ff00ff"
                android:text="上海"
                android:textSize="20px"
                android:layout_weight="0.25"
                android:textColor="#FFFAFA" />

            <Button
                android:id="@+id/city1_3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#00ff00ff"
                android:text="广州"
                android:textSize="20px"
                android:layout_weight="0.25"
                android:textColor="#FFFAFA" />

            <Button
                android:id="@+id/city1_4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#00ff00ff"
                android:text="深圳"
                android:textSize="20px"
                android:layout_weight="0.25"
                android:textColor="#FFFAFA" />
		</TableRow>
	</TableLayout>
	<TextView android:text="" 
		android:id="@+id/TextView01"
		android:layout_width="wrap_content" 
		android:layout_height="wrap_content"
		android:textColor="#000000"
		android:textSize="20px"/>
	<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
		android:layout_width="wrap_content" 
		android:layout_height="wrap_content"
		android:orientation="horizontal">
	<ImageView android:id="@+id/image1"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"/>
	<ImageView android:id="@+id/image2"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"/>
	</LinearLayout>
</LinearLayout>

设置EditText边框颜色、透明
drawable/shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#00ff00ff"/>
    <corners android:radius="8px"/>
    <stroke android:width="3px"
        android:color="#0000CD"/>
    
</shape>


 

AndroidManifest.xml中添加:

<uses-permission android:name="android.permission.INTERNET">
</uses-permission>


 

你可能感兴趣的:(android webservice获取城市天气情况)