现在的位置: 首页 > web前端 > 正文

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

2018年09月02日 web前端 ⁄ 共 6325字 ⁄ 字号 评论关闭

实现效果:

 

 

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

布局文件main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:text="输入城市:"
            android:textSize="20dp" />

        <EditText
            android:id="@+id/city"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="fill_horizontal" >
        </EditText>
    </LinearLayout>

    <Button
        android:id="@+id/search"
        android:layout_gravity="right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="查询天气" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="top"
            android:text="天气情况:"
            android:textSize="20dp" />

        <EditText
            android:id="@+id/state"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textMultiLine"
            android:minLines="4" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="明日天气:"
            android:textSize="20dp" />

        <ImageView
            android:id="@+id/image"
            android:layout_width="190dp"
            android:layout_height="160dp"
            android:scaleType="fitXY" />
    </LinearLayout>

</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");

代码:

public class WeatherStateSearch {
	public static SoapObject searchWea(String wsdlurl,String method,String cityname) {
		//指定webservice的命名空间和调用的方法名
		String namespace="http://WebXml.com.cn/";
		SoapObject  soap=new SoapObject(namespace,method);
		//添加属性,只要设置参数的顺序一致,调用方法的参数名不一定与服务端的WebService类中的方法参数名一致
		soap.addProperty("theCityName",cityname);
		//通过SoapSerializationEnvelope类的构造方法设置SOAP协议的版本号。 
		SoapSerializationEnvelope soapEnvelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
		//设置需要传出的Soap
		soapEnvelope.bodyOut=soap;
		soapEnvelope.dotNet=true;
		soapEnvelope.setOutputSoapObject(soap);
		//创建http传输对象
		HttpTransportSE transportSE=new HttpTransportSE(wsdlurl);
		//soap操作url
		String SOAP_ACTION=namespace+method;
		try {
			//请求调用WebService方法
			transportSE.call(SOAP_ACTION, soapEnvelope);
			//使用getResponse获得WebService方法解析xml的返回结果
			SoapObject result=(SoapObject) soapEnvelope.getResponse();
			if(result!=null)
				return result;
		} catch (IOException e) {
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		}
		return null;
	}
}

 

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

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

public class MainActivity extends Activity {
	private EditText city;
	private Button search;
	private EditText weastate;
	private ImageView img;
	 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      //查找组件
        city=(EditText) this.findViewById(R.id.city);
        search=(Button) this.findViewById(R.id.search);
        weastate=(EditText) this.findViewById(R.id.state);
        img=(ImageView) this.findViewById(R.id.image);
        search.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				String cname=city.getText().toString();
				//web服务端天气预报url
				String wsdlUrl="http://www.webxml.com.cn/webservices/weatherwebservice.asmx";
				//调用web提供的方法
				SoapObject weather=WeatherStateSearch.searchWea(wsdlUrl,"getWeatherbyCityName",cname);
				String state=weather.getProperty(10).toString();
				System.out.println(state);
				String strIcon=weather.getProperty(15).toString();
				//查询结果显示在结果文本域
				weastate.setText(state);
                                //设置天气图片
				img.setImageResource(parseIcon(strIcon));
			}
		});
    }
    //查到的图片转换为项目中对应的int类型值
	private int parseIcon(String strIcon) {
		if ("0.gif".equals(strIcon)) return R.drawable.a_0;
		if ("1.gif".equals(strIcon)) return R.drawable.a_1;
		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;
	}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

总结一下:

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

 

 

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

抱歉!评论已关闭.