欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

wenserver获取天气预报数据实例分享

程序员文章站 2022-07-11 19:33:17
复制代码 代码如下:package tao.cs; import java.io.ioexception; import org.ksoap2.soapenvelope...

复制代码 代码如下:

package tao.cs;

import java.io.ioexception;

import org.ksoap2.soapenvelope;
import org.ksoap2.serialization.soapobject;
import org.ksoap2.serialization.soapserializationenvelope;
import org.ksoap2.transport.androidhttptransport;
import org.xmlpull.v1.xmlpullparserexception;

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 weathermain extends activity {
    button btn01;  
    textview tv01,tv02;  
    imageview imgview01,imgview02;  
    edittext et01;  

    int int_img_1;  
    int int_img_2;  

     public void oncreate(bundle savedinstancestate) {  
         super.oncreate(savedinstancestate);  
         setcontentview(r.layout.main);  

         //组件  
       //  final edittext et01=(edittext) findviewbyid(r.id.et_textweather);  
         btn01=(button) findviewbyid(r.id.button01);  
         tv01=(textview) findviewbyid(r.id.textview01);  
         tv02=(textview) findviewbyid(r.id.textview02);  
         imgview01=(imageview) findviewbyid(r.id.imageview01);  
         imgview02=(imageview) findviewbyid(r.id.imageview02);  
         et01=(edittext) findviewbyid(r.id.edittext01);  

         btn01.setonclicklistener(new onclicklistener() {  

             public void onclick(view v) {  
                 showweather();  

             }  
         });  

           
     }//oncreate ***************end  
     protected void showweather(){//显示所有的信息  

         string str_city=et01.gettext().tostring();  
         if(str_city.length()==0){  
             str_city="重庆";  

         }  
//         str_city="济南";  
         getweather(str_city);  
         tv01.settext(getstring_weathertoday());  //今天天气  
         tv02.settext(getstring_weathercurrent());//当前天气  
         imgview01.setimageresource(geticon_1());  //当前的两个图标  
         imgview02.setimageresource(geticon_2());  

     }//showweather()*****************end  

     private static final string name_space="http://webxml.com.cn/";//命名空间  ,后期测试一下命名空间的作用  
     private static final string method_name = "getweatherbycityname";  
     private static string url = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx";// 投递soap数据的目标地址  
     private static string soap_action = "http://webxml.com.cn/getweatherbycityname";  

     protected void getweather(string strcityname){//str 是城市名  
  /* 
   * 这里要做的工作是 提交客户端的请求, 
   * 然后获得服务端返回的复杂对象,后面会有专门的 
   */ 
         soapobject sobject=new soapobject(name_space, method_name);  
         sobject.addproperty("thecityname", strcityname);  

         androidhttptransport ht=new androidhttptransport(url);  
         ht.debug=true;  

         soapserializationenvelope envelope=new soapserializationenvelope(soapenvelope.ver11);//告诉soapserializationenvelope把构造好的soapobject封装进去:  
         envelope.bodyout=sobject;  
         envelope.dotnet=true;  
         envelope.setoutputsoapobject(sobject);  

         try {  

             ht.call(soap_action, envelope);  

         } catch (ioexception e) {  

             e.printstacktrace();  
         } catch (xmlpullparserexception e) {  

             e.printstacktrace();  
         }  
         // 
         soapobject result=(soapobject) envelope.bodyin;  
         soapobject detail=(soapobject) result.getproperty("getweatherbycitynameresult");//类似于获取服务端返回复杂节点的一个内接点  
         parseweather(detail);   

     }//getweather *****************end  

     string str_weather_today=null;  
     string str_weather_current=null;  
     protected void parseweather(soapobject so){  
         /* 
          * 解析复杂节点,并对相应的参数赋值,为后面的方法准备 
          */ 
         //第六个参数是概况,包括日期,天气....  
         string date=so.getproperty(6).tostring();  

         str_weather_today="今天: "+date.split(" ")[0];  
         str_weather_today=str_weather_today+"\n气温:"+so.getproperty(5).tostring();  
         str_weather_today=str_weather_today+"\n风力:"+so.getproperty(7).tostring()+"\n";  
         //两个图标  
         int_img_1=parseicon(so.getproperty(8).tostring());  
         int_img_2=parseicon(so.getproperty(9).tostring());  

         str_weather_current=so.getproperty(10).tostring();  
     }  
     public string getstring_weathertoday(){  
         return str_weather_today;  
     }  
     public string getstring_weathercurrent(){  
         return str_weather_current;  
     }  

     public int geticon_1(){  
         return int_img_1;  
     }  
     public int geticon_2(){  
         return int_img_2;  
     }  
     public int   parseicon(string stricon){  
         if (stricon == null) return -1;  

         if ("0.gif".equals(stricon)) return r.drawable.a_0;  
         if ("1.gif".equals(stricon)) return r.drawable.a_1;  
         if ("2.gif".equals(stricon)) return r.drawable.a_2;  
         if ("3.gif".equals(stricon)) return r.drawable.a_3;  
         if ("4.gif".equals(stricon)) return r.drawable.a_4;  
         if ("5.gif".equals(stricon)) return r.drawable.a_5;  
         if ("6.gif".equals(stricon)) return r.drawable.a_6;  
         if ("7.gif".equals(stricon)) return r.drawable.a_7;  
         if ("8.gif".equals(stricon)) return r.drawable.a_8;  
         if ("9.gif".equals(stricon)) return r.drawable.a_9;  
         if ("10.gif".equals(stricon)) return r.drawable.a_10;  
         if ("11.gif".equals(stricon)) return r.drawable.a_11;  
         if ("12.gif".equals(stricon)) return r.drawable.a_12;  
         if ("13.gif".equals(stricon)) return r.drawable.a_13;  
         if ("14.gif".equals(stricon)) return r.drawable.a_14;  
         if ("15.gif".equals(stricon)) return r.drawable.a_15;  
         if ("16.gif".equals(stricon)) return r.drawable.a_16;  
         if ("17.gif".equals(stricon)) return r.drawable.a_17;  
         if ("18.gif".equals(stricon)) return r.drawable.a_18;  
         if ("19.gif".equals(stricon)) return r.drawable.a_19;  
         if ("20.gif".equals(stricon)) return r.drawable.a_20;  
         if ("21.gif".equals(stricon)) return r.drawable.a_21;  
         if ("22.gif".equals(stricon)) return r.drawable.a_22;  
         if ("23.gif".equals(stricon)) return r.drawable.a_23;  
         if ("24.gif".equals(stricon)) return r.drawable.a_24;  
         if ("25.gif".equals(stricon)) return r.drawable.a_25;  
         if ("26.gif".equals(stricon)) return r.drawable.a_26;  
         if ("27.gif".equals(stricon)) return r.drawable.a_27;  
         if ("28.gif".equals(stricon)) return r.drawable.a_28;  
         if ("29.gif".equals(stricon)) return r.drawable.a_29;  
         if ("30.gif".equals(stricon)) return r.drawable.a_30;  
         if ("31.gif".equals(stricon)) return r.drawable.a_31;  

         return 0;  
     }


}

复制代码 代码如下:

<?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"
    >
<edittext

 android:id="@+id/edittext01"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content">
</edittext>
<button
 android:text="查询"
 android:id="@+id/button01"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content">
</button>
<imageview
 android:id="@+id/imageview01"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content">
</imageview>
<imageview
 android:id="@+id/imageview02"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content">
</imageview>
<textview 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textview01"

    />
<textview

 android:id="@+id/textview02"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content">
</textview>


</linearlayout>