android webservice获取城市天气情况

Weather.java

[java]  view plain copy
  1. package com.example;  
  2.   
  3. import java.io.UnsupportedEncodingException;  
  4.   
  5. import org.ksoap2.SoapEnvelope;  
  6. import org.ksoap2.serialization.SoapObject;  
  7. import org.ksoap2.serialization.SoapSerializationEnvelope;  
  8.   
  9. import org.ksoap2.transport.HttpTransportSE;  
  10.   
  11.   
  12. import android.app.Activity;  
  13. import android.os.Bundle;  
  14. import android.view.View;  
  15. import android.view.View.OnClickListener;  
  16. import android.widget.Button;  
  17. import android.widget.EditText;  
  18. import android.widget.ImageView;  
  19. import android.widget.TextView;  
  20.   
  21. public class Weather extends Activity {  
  22.   
  23.   
  24.   
  25.      private Button search_but;  
  26.      private TextView textview1;  
  27.      private EditText et;  
  28.      private Button city1_1;  
  29.      private Button city1_2;  
  30.      private Button city1_3;  
  31.      private Button city1_4;  
  32.      private ImageView image1;  
  33.      private ImageView image2;  
  34.      /** Called when the activity is first created. */  
  35.      @Override  
  36.      public void onCreate(Bundle savedInstanceState) {  
  37.       super.onCreate(savedInstanceState);  
  38.       setContentView(R.layout.main);  
  39.   
  40.      et=(EditText)findViewById(R.id.et);  
  41.      image1=(ImageView)findViewById(R.id.image1);  
  42.      image2=(ImageView)findViewById(R.id.image2);  
  43.      search_but=(Button)findViewById(R.id.search_but);  
  44.      city1_1=(Button)findViewById(R.id.city1_1);  
  45.      city1_1.setOnClickListener(listener1);  
  46.      city1_2=(Button)findViewById(R.id.city1_2);  
  47.      city1_2.setOnClickListener(listener2);  
  48.      city1_3=(Button)findViewById(R.id.city1_3);  
  49.      city1_3.setOnClickListener(listener3);  
  50.      city1_4=(Button)findViewById(R.id.city1_4);  
  51.      city1_4.setOnClickListener(listener4);  
  52.      search_but.setOnClickListener(listener);  
  53.      }  
  54.      private OnClickListener listener=new OnClickListener() {  
  55.           
  56.         @Override  
  57.         public void onClick(View v) {  
  58.             String city=et.getText().toString();  
  59.             getWeather(city);  
  60.         }  
  61.     };   
  62.     private OnClickListener listener1=new OnClickListener() {  
  63.           
  64.         @Override  
  65.         public void onClick(View v) {  
  66.             et.setText("北京");  
  67.         }  
  68.     };  
  69.     private OnClickListener listener2=new OnClickListener() {  
  70.           
  71.         @Override  
  72.         public void onClick(View v) {  
  73.             et.setText("上海");  
  74.         }  
  75.     };  
  76.     private OnClickListener listener3=new OnClickListener() {  
  77.           
  78.         @Override  
  79.         public void onClick(View v) {  
  80.             et.setText("深圳");  
  81.         }  
  82.     };  
  83.     private OnClickListener listener4=new OnClickListener() {  
  84.           
  85.         @Override  
  86.         public void onClick(View v) {  
  87.             et.setText("北京");  
  88.         }  
  89.     };  
  90.       
  91.   
  92.      private static final String NAMESPACE = "http://WebXml.com.cn/";  
  93.   
  94.      // WebService地址  
  95.      private static String URL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx";  
  96.   
  97.      private static final String METHOD_NAME = "getWeatherbyCityName";  
  98.   
  99.      private static String SOAP_ACTION = "http://WebXml.com.cn/getWeatherbyCityName";  
  100.   
  101.      private String weatherToday;  
  102.   
  103.      private SoapObject detail;  
  104.   
  105.      private String weatherNow;  
  106.        
  107.      private String weatherWillBe;  
  108.        
  109.      private void setIcon(String weather, ImageView imageview) {  
  110.             if(weather.equalsIgnoreCase("nothing.gif"))  
  111.                 imageview.setBackgroundResource(R.drawable.a_nothing);  
  112.             if(weather.equalsIgnoreCase("0.gif"))  
  113.                 imageview.setBackgroundResource(R.drawable.a_0);  
  114.             if(weather.equalsIgnoreCase("1.gif"))  
  115.                 imageview.setBackgroundResource(R.drawable.a_1);  
  116.             if(weather.equalsIgnoreCase("2.gif"))  
  117.                 imageview.setBackgroundResource(R.drawable.a_2);  
  118.             if(weather.equalsIgnoreCase("3.gif"))  
  119.                 imageview.setBackgroundResource(R.drawable.a_3);          
  120.             if(weather.equalsIgnoreCase("4.gif"))  
  121.                 imageview.setBackgroundResource(R.drawable.a_4);  
  122.             if(weather.equalsIgnoreCase("5.gif"))  
  123.                 imageview.setBackgroundResource(R.drawable.a_5);  
  124.             if(weather.equalsIgnoreCase("6.gif"))  
  125.                 imageview.setBackgroundResource(R.drawable.a_6);  
  126.             if(weather.equalsIgnoreCase("7.gif"))  
  127.                 imageview.setBackgroundResource(R.drawable.a_7);  
  128.             if(weather.equalsIgnoreCase("8.gif"))  
  129.                 imageview.setBackgroundResource(R.drawable.a_8);  
  130.             if(weather.equalsIgnoreCase("9.gif"))  
  131.                 imageview.setBackgroundResource(R.drawable.a_9);  
  132.             if(weather.equalsIgnoreCase("10.gif"))  
  133.                 imageview.setBackgroundResource(R.drawable.a_10);             
  134.             if(weather.equalsIgnoreCase("11.gif"))  
  135.                 imageview.setBackgroundResource(R.drawable.a_11);  
  136.             if(weather.equalsIgnoreCase("12.gif"))  
  137.                 imageview.setBackgroundResource(R.drawable.a_12);  
  138.             if(weather.equalsIgnoreCase("13.gif"))  
  139.                 imageview.setBackgroundResource(R.drawable.a_13);  
  140.             if(weather.equalsIgnoreCase("14.gif"))  
  141.                 imageview.setBackgroundResource(R.drawable.a_14);  
  142.             if(weather.equalsIgnoreCase("15.gif"))  
  143.                 imageview.setBackgroundResource(R.drawable.a_15);  
  144.             if(weather.equalsIgnoreCase("16.gif"))  
  145.                 imageview.setBackgroundResource(R.drawable.a_16);  
  146.             if(weather.equalsIgnoreCase("17.gif"))  
  147.                 imageview.setBackgroundResource(R.drawable.a_17);  
  148.             if(weather.equalsIgnoreCase("18.gif"))  
  149.                 imageview.setBackgroundResource(R.drawable.a_18);  
  150.             if(weather.equalsIgnoreCase("19.gif"))  
  151.                 imageview.setBackgroundResource(R.drawable.a_19);  
  152.             if(weather.equalsIgnoreCase("20.gif"))  
  153.                 imageview.setBackgroundResource(R.drawable.a_20);  
  154.             if(weather.equalsIgnoreCase("21.gif"))  
  155.                 imageview.setBackgroundResource(R.drawable.a_21);  
  156.             if(weather.equalsIgnoreCase("22.gif"))  
  157.                 imageview.setBackgroundResource(R.drawable.a_22);  
  158.             if(weather.equalsIgnoreCase("23.gif"))  
  159.                 imageview.setBackgroundResource(R.drawable.a_23);  
  160.             if(weather.equalsIgnoreCase("24.gif"))  
  161.                 imageview.setBackgroundResource(R.drawable.a_24);  
  162.             if(weather.equalsIgnoreCase("25.gif"))  
  163.                 imageview.setBackgroundResource(R.drawable.a_25);  
  164.             if(weather.equalsIgnoreCase("26.gif"))  
  165.                 imageview.setBackgroundResource(R.drawable.a_26);  
  166.             if(weather.equalsIgnoreCase("27.gif"))  
  167.                 imageview.setBackgroundResource(R.drawable.a_27);  
  168.             if(weather.equalsIgnoreCase("28.gif"))  
  169.                 imageview.setBackgroundResource(R.drawable.a_28);  
  170.             if(weather.equalsIgnoreCase("29.gif"))  
  171.                 imageview.setBackgroundResource(R.drawable.a_29);  
  172.             if(weather.equalsIgnoreCase("30.gif"))  
  173.                 imageview.setBackgroundResource(R.drawable.a_30);             
  174.             if(weather.equalsIgnoreCase("31.gif"))  
  175.                 imageview.setBackgroundResource(R.drawable.a_31);  
  176.         }  
  177.      public void getWeather(String cityName) {  
  178.       try {  
  179.        textview1 = (TextView) this.findViewById(R.id.TextView01);  
  180.        SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);  
  181.        rpc.addProperty("theCityName", cityName);  
  182.   
  183.        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(  
  184.          SoapEnvelope.VER11);  
  185.        envelope.bodyOut = rpc;  
  186.        envelope.dotNet = true;  
  187.        envelope.setOutputSoapObject(rpc);  
  188.        HttpTransportSE ht = new HttpTransportSE(URL);  
  189.   
  190.        ht.debug = true;  
  191.   
  192.        ht.call(SOAP_ACTION, envelope);  
  193.   
  194.        detail = (SoapObject) envelope.getResponse();  
  195.        parseWeather(detail);  
  196.   
  197.        return;  
  198.       } catch (Exception e) {  
  199.        e.printStackTrace();  
  200.       }  
  201.      }  
  202.   
  203.   
  204.        
  205.      private void parseWeather(SoapObject detail)  
  206.       throws UnsupportedEncodingException {  
  207.       textview1 = (TextView) this.findViewById(R.id.TextView01);  
  208.   
  209.       String date = detail.getProperty(6).toString();  
  210.         
  211.         
  212.       weatherToday = "\n天气:" + date.split(" ")[1];  
  213.       weatherToday = weatherToday + "\n气温:"  
  214.         + detail.getProperty(5).toString();  
  215.       weatherToday = weatherToday + "\n风力:"  
  216.         + detail.getProperty(7).toString() + "\n";  
  217.       weatherNow = detail.getProperty(8).toString();  
  218.       weatherWillBe = detail.getProperty(9).toString();  
  219.   
  220.       textview1.setText(et.getText() + weatherToday);  
  221.       setIcon(weatherNow, image1);  
  222.       setIcon(weatherWillBe, image2);  
  223.   
  224.      }  
  225.     }  


 

main.xml

[java]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"   
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"   
  6.     android:background="@drawable/bg">  
  7.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  8.         android:layout_width="fill_parent"   
  9.         android:layout_height="wrap_content"  
  10.         android:orientation="horizontal">  
  11.   
  12.         <EditText android:id="@+id/et"   
  13.         android:layout_width="237dp"  
  14.             android:layout_height="50dp"   
  15.             android:background="@drawable/shape"  
  16.             android:drawableLeft="@drawable/title"   
  17.             android:hint="北京/beijing/bj"  
  18.             android:padding="10px"   
  19.             android:singleLine="true"   
  20.             android:textColor="#FFFAFA" />  
  21.   
  22.         <Button android:id="@+id/search_but"   
  23.         android:layout_width="wrap_content"  
  24.             android:layout_height="wrap_content"  
  25.              android:background="#00ff00ff"  
  26.             android:text="Search"   
  27.             android:textColor="#696969"   
  28.             android:textStyle="italic" />  
  29.   
  30.     </LinearLayout>  
  31.     <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  32.         android:layout_width="fill_parent"   
  33.         android:layout_height="wrap_content"  
  34.         android:orientation="horizontal">  
  35.         <TableRow>  
  36.             <Button  
  37.                 android:id="@+id/city1_1"  
  38.                 android:layout_width="wrap_content"  
  39.                 android:layout_height="wrap_content"  
  40.                 android:background="#00000000"  
  41.                 android:text="北京"  
  42.                 android:layout_weight="0.25"  
  43.                 android:textSize="20px"  
  44.                 android:textColor="#FFFAFA" />  
  45.   
  46.             <Button  
  47.                 android:id="@+id/city1_2"  
  48.                 android:layout_width="wrap_content"  
  49.                 android:layout_height="wrap_content"  
  50.                 android:background="#00ff00ff"  
  51.                 android:text="上海"  
  52.                 android:textSize="20px"  
  53.                 android:layout_weight="0.25"  
  54.                 android:textColor="#FFFAFA" />  
  55.   
  56.             <Button  
  57.                 android:id="@+id/city1_3"  
  58.                 android:layout_width="wrap_content"  
  59.                 android:layout_height="wrap_content"  
  60.                 android:background="#00ff00ff"  
  61.                 android:text="广州"  
  62.                 android:textSize="20px"  
  63.                 android:layout_weight="0.25"  
  64.                 android:textColor="#FFFAFA" />  
  65.   
  66.             <Button  
  67.                 android:id="@+id/city1_4"  
  68.                 android:layout_width="wrap_content"  
  69.                 android:layout_height="wrap_content"  
  70.                 android:background="#00ff00ff"  
  71.                 android:text="深圳"  
  72.                 android:textSize="20px"  
  73.                 android:layout_weight="0.25"  
  74.                 android:textColor="#FFFAFA" />  
  75.         </TableRow>  
  76.     </TableLayout>  
  77.     <TextView android:text=""   
  78.         android:id="@+id/TextView01"  
  79.         android:layout_width="wrap_content"   
  80.         android:layout_height="wrap_content"  
  81.         android:textColor="#000000"  
  82.         android:textSize="20px"/>  
  83.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  84.         android:layout_width="wrap_content"   
  85.         android:layout_height="wrap_content"  
  86.         android:orientation="horizontal">  
  87.     <ImageView android:id="@+id/image1"  
  88.     android:layout_width="wrap_content"  
  89.     android:layout_height="wrap_content"/>  
  90.     <ImageView android:id="@+id/image2"  
  91.     android:layout_width="wrap_content"  
  92.     android:layout_height="wrap_content"/>  
  93.     </LinearLayout>  
  94. </LinearLayout>  

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

[java]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <solid android:color="#00ff00ff"/>  
  4.     <corners android:radius="8px"/>  
  5.     <stroke android:width="3px"  
  6.         android:color="#0000CD"/>  
  7.       
  8. </shape>  


 

AndroidManifest.xml中添加:

[java]  view plain copy
  1. <uses-permission android:name="android.permission.INTERNET">  
  2. </uses-permission>  
本文转载来自 liushengmeng http://blog.csdn.net/liushengmeng/article/details/8128524
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值