android使用soap协议访问webservice实现天气预报功能

来源:互联网 发布:即将开放的新后缀域名 编辑:程序博客网 时间:2024/06/05 04:21

http://blog.csdn.net/luran_fighting/article/details/8247278

实现效果:

 

 

首先创建布局文件,显示出需要查找的天气情况,可以查出今天明天或者后天的天气情况。将需要的信息显示出来,想要显示查找的城市图片,要把所有的城市照片以城市代号命名的图片都要存储到项目中,数据量大,所以这里只是显示出天气的图片,共有三十二张,可以网上下载,记住顺序一定不要弄错。

布局文件main.xml

[html] view plaincopyprint?
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"  
  6.     android:padding="10dp" >  
  7.   
  8.     <LinearLayout  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:orientation="horizontal" >  
  12.   
  13.         <TextView  
  14.             android:layout_width="wrap_content"  
  15.             android:layout_height="wrap_content"  
  16.             android:gravity="left"  
  17.             android:text="输入城市:"  
  18.             android:textSize="20dp" />  
  19.   
  20.         <EditText  
  21.             android:id="@+id/city"  
  22.             android:layout_width="fill_parent"  
  23.             android:layout_height="wrap_content"  
  24.             android:gravity="fill_horizontal" >  
  25.         </EditText>  
  26.     </LinearLayout>  
  27.   
  28.     <Button  
  29.         android:id="@+id/search"  
  30.         android:layout_gravity="right"  
  31.         android:layout_width="wrap_content"  
  32.         android:layout_height="wrap_content"  
  33.         android:text="查询天气" />  
  34.   
  35.     <LinearLayout  
  36.         android:layout_width="fill_parent"  
  37.         android:layout_height="wrap_content"  
  38.         android:orientation="horizontal" >  
  39.   
  40.         <TextView  
  41.             android:layout_width="wrap_content"  
  42.             android:layout_height="wrap_content"  
  43.             android:gravity="top"  
  44.             android:text="天气情况:"  
  45.             android:textSize="20dp" />  
  46.   
  47.         <EditText  
  48.             android:id="@+id/state"  
  49.             android:layout_width="fill_parent"  
  50.             android:layout_height="wrap_content"  
  51.             android:ems="10"  
  52.             android:inputType="textMultiLine"  
  53.             android:minLines="4" />  
  54.     </LinearLayout>  
  55.   
  56.     <LinearLayout  
  57.         android:layout_width="fill_parent"  
  58.         android:layout_height="wrap_content"  
  59.         android:orientation="horizontal" >  
  60.   
  61.         <TextView  
  62.             android:layout_width="wrap_content"  
  63.             android:layout_height="wrap_content"  
  64.             android:text="明日天气:"  
  65.             android:textSize="20dp" />  
  66.   
  67.         <ImageView  
  68.             android:id="@+id/image"  
  69.             android:layout_width="190dp"  
  70.             android:layout_height="160dp"  
  71.             android:scaleType="fitXY" />  
  72.     </LinearLayout>  
  73.   
  74. </LinearLayout>  


2.然后编写activity代码,因为要用到Soap协议,所以首先要到网站下载soap的jar包,这里用到的是ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar,因为比较高的版本,将jar包粘贴到项目libs文件下,环境会自动匹配将jar包加载,会在项目下的dependencies下找到soap包,则添加成功。

 

3.注意要用到网上资源解析xml,则在AndroidManifest.xml中设置用户权限

 

4.编写通过soap协议发送天气预报请求使用web服务得到并返回结果的工具类。

    获得天气状况

      (1)getResponse()方法查天气预报得到的是一系列的值,不是单一值,所以返回结果用SoapObject接收,调用它的getProperty()得到需要的信息,以下是getWeatherbyCityNameResult的23个属性:

 

      

     (2)获取数组这些属性值也可用另一种方法:

      SoapObject result = (SoapObject) envelope.bodyIn;
      SoapObject detail = (SoapObject) result.getProperty("getWeatherbyCityNameResult");

代码:

[java] view plaincopyprint?
  1. <span style="font-size:14px;">public class WeatherStateSearch {  
  2.     public static SoapObject searchWea(String wsdlurl,String method,String cityname) {  
  3.         //指定webservice的命名空间和调用的方法名  
  4.         String namespace="http://WebXml.com.cn/";  
  5.         SoapObject  soap=new SoapObject(namespace,method);  
  6.         //添加属性,只要设置参数的顺序一致,调用方法的参数名不一定与服务端的WebService类中的方法参数名一致  
  7.         soap.addProperty("theCityName",cityname);  
  8.         //通过SoapSerializationEnvelope类的构造方法设置SOAP协议的版本号。   
  9.         SoapSerializationEnvelope soapEnvelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);  
  10.         //设置需要传出的Soap  
  11.         soapEnvelope.bodyOut=soap;  
  12.         soapEnvelope.dotNet=true;  
  13.         soapEnvelope.setOutputSoapObject(soap);  
  14.         //创建http传输对象  
  15.         HttpTransportSE transportSE=new HttpTransportSE(wsdlurl);  
  16.         //soap操作url  
  17.         String SOAP_ACTION=namespace+method;  
  18.         try {  
  19.             //请求调用WebService方法  
  20.             transportSE.call(SOAP_ACTION, soapEnvelope);  
  21.             //使用getResponse获得WebService方法解析xml的返回结果  
  22.             SoapObject result=(SoapObject) soapEnvelope.getResponse();  
  23.             if(result!=null)  
  24.                 return result;  
  25.         } catch (IOException e) {  
  26.             e.printStackTrace();  
  27.         } catch (XmlPullParserException e) {  
  28.             e.printStackTrace();  
  29.         }  
  30.         return null;  
  31.     }  
  32. }  
  33.   
  34. </span>  


 

5.项目main类的关键代码:

web服务端天气预报url:"http://www.webxml.com.cn/webservices/weatherwebservice.asmx";

[java] view plaincopyprint?
  1. public class MainActivity extends Activity {  
  2.     private EditText city;  
  3.     private Button search;  
  4.     private EditText weastate;  
  5.     private ImageView img;  
  6.        
  7.     @Override  
  8.     public void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.activity_main);  
  11.       //查找组件  
  12.         city=(EditText) this.findViewById(R.id.city);  
  13.         search=(Button) this.findViewById(R.id.search);  
  14.         weastate=(EditText) this.findViewById(R.id.state);  
  15.         img=(ImageView) this.findViewById(R.id.image);  
  16.         search.setOnClickListener(new OnClickListener() {  
  17.             public void onClick(View v) {  
  18.                 <strong><span style="font-size:12px;">String cname=city.getText().toString();  
  19.                 //web服务端天气预报url  
  20.                 String wsdlUrl="http://www.webxml.com.cn/webservices/weatherwebservice.asmx";  
  21.                 //调用web提供的方法  
  22.                 SoapObject weather=WeatherStateSearch.searchWea(wsdlUrl,"getWeatherbyCityName",cname);  
  23.                 String state=weather.getProperty(10).toString();  
  24.                 System.out.println(state);  
  25.                 String strIcon=weather.getProperty(15).toString();  
  26.                 //查询结果显示在结果文本域  
  27.                 weastate.setText(state);  
  28.                                 //设置天气图片</span></strong>  
[java] view plaincopyprint?
  1. <strong><span style="font-size:12px;">              img.setImageResource(parseIcon(strIcon));  
  2. </span></strong>            }  
  3.         });  
  4.     }  
  5.     //查到的图片转换为项目中对应的int类型值  
  6.     private int parseIcon(String strIcon) {  
  7.         if ("0.gif".equals(strIcon)) return R.drawable.a_0;  
  8.         if ("1.gif".equals(strIcon)) return R.drawable.a_1;  
  9.         if ("3.gif".equals(strIcon)) return R.drawable.a_3;  
  10.         if ("4.gif".equals(strIcon)) return R.drawable.a_4;  
  11.         if ("5.gif".equals(strIcon)) return R.drawable.a_5;  
  12.         if ("6.gif".equals(strIcon)) return R.drawable.a_6;  
  13.         if ("7.gif".equals(strIcon)) return R.drawable.a_7;  
  14.         if ("8.gif".equals(strIcon)) return R.drawable.a_8;  
  15.         if ("9.gif".equals(strIcon)) return R.drawable.a_9;  
  16.         if ("10.gif".equals(strIcon)) return R.drawable.a_10;  
  17.         if ("11.gif".equals(strIcon)) return R.drawable.a_11;  
  18.         if ("12.gif".equals(strIcon)) return R.drawable.a_12;  
  19.         if ("13.gif".equals(strIcon)) return R.drawable.a_13;  
  20.         if ("14.gif".equals(strIcon)) return R.drawable.a_14;  
  21.         if ("15.gif".equals(strIcon)) return R.drawable.a_15;  
  22.         if ("16.gif".equals(strIcon)) return R.drawable.a_16;  
  23.         if ("17.gif".equals(strIcon)) return R.drawable.a_17;  
  24.         if ("18.gif".equals(strIcon)) return R.drawable.a_18;  
  25.         if ("19.gif".equals(strIcon)) return R.drawable.a_19;  
  26.         if ("20.gif".equals(strIcon)) return R.drawable.a_20;  
  27.         if ("21.gif".equals(strIcon)) return R.drawable.a_21;  
  28.         if ("22.gif".equals(strIcon)) return R.drawable.a_22;  
  29.         if ("23.gif".equals(strIcon)) return R.drawable.a_23;  
  30.         if ("24.gif".equals(strIcon)) return R.drawable.a_24;  
  31.         if ("25.gif".equals(strIcon)) return R.drawable.a_25;  
  32.         if ("26.gif".equals(strIcon)) return R.drawable.a_26;  
  33.         if ("27.gif".equals(strIcon)) return R.drawable.a_27;  
  34.         if ("28.gif".equals(strIcon)) return R.drawable.a_28;  
  35.         if ("29.gif".equals(strIcon)) return R.drawable.a_29;  
  36.         if ("30.gif".equals(strIcon)) return R.drawable.a_30;  
  37.         if ("31.gif".equals(strIcon)) return R.drawable.a_31;  
  38.         return 0;  
  39.     }  
  40.   
  41.     @Override  
  42.     public boolean onCreateOptionsMenu(Menu menu) {  
  43.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  44.         return true;  
  45.     }  
  46. }  
  47. <span style="font-size:18px;"></span>  

总结一下:

         在做项目时总在main中出现空指针异常,在设置天气图片资源的时候提示错误,后来经过分析测试,程序能够查到结果,并正确输出,我又看了遍程序原来没有查找图片视图组件,真是出了很低级的错误,下次定要认真,开始时就要看全代码,感觉自己写了其实还是没写,要确认一下才行,这样编程序才更有效率,错误少,完成时间短,才是一个成功的程序师。

 

 

呵呵……无论编写什么程序都会提高自己找错、纠错、少错的能力,提高编程能力,加油!!!

0 0
原创粉丝点击