android调用Webservice——天气情况查询

本文介绍了一个简单的Android应用,通过WebService接口实现城市天气查询的功能。详细展示了应用界面布局、SOAP请求封装及响应处理的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

利用Webservice连接网络进行实时查询,可以根据手机号码查询归属地也可根据城市名称查询天气情况。两者相差不多,这里先做下天气情况的查询:

首先,对展示的页面进行布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="@drawable/a"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="@dimen/padding_medium"
        android:text="@string/cityname"
        tools:context=".MainActivity" />
    <EditText 
        android:id="@+id/cityname"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <Button 
        android:id="@+id/search"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/search"/>
    <TextView 
        android:id="@+id/result"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>


其次,对方法进行封装(很重要哦,一个标点都不能出错)

public class SOAPUtil {
	public static Object doTransport(final String wsdUrl,final String webMethod,String city){
	         String nameSpace="http://WebXml.com.cn/";
		SoapObject soapObject=new SoapObject(nameSpace, webMethod);
		soapObject.addProperty("theCityName", city);
		System.out.println(city);
		SoapSerializationEnvelope soapSerializationEnvelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapSerializationEnvelope.bodyOut=soapObject;
		soapSerializationEnvelope.dotNet=true;
		soapSerializationEnvelope.setOutputSoapObject(soapObject);
		HttpTransportSE httpTransportSE=new HttpTransportSE(wsdUrl);
		
		String SOAP_ACTION="http://WebXml.com.cn/"+webMethod;
		System.out.println(SOAP_ACTION);
		try{
			httpTransportSE.call(SOAP_ACTION, soapSerializationEnvelope);
			System.out.println("调用结束");
			System.out.println(soapSerializationEnvelope.getResponse());
			if(soapSerializationEnvelope.getResponse()!=null){
				Object result=soapSerializationEnvelope.getResponse();
				System.out.println(result);
				return result;
			}
		}catch(IOException e){
			System.out.println("IOException");
			e.printStackTrace();
		}catch(XmlPullParserException e){
			e.printStackTrace();
		}		
		
		
		
		
		return null;
		
	}

}

主要方法的调用了,很简单,除了点击事件作出相应的改变,其他的都和最初学习的一样:

public class MainActivity extends Activity {
	private EditText city;
	private Button searchs;
	private TextView results;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        city=(EditText) findViewById(R.id.cityname);
        searchs=(Button) findViewById(R.id.search);
        results= (TextView) findViewById(R.id.result);
        searchs.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String cityname=city.getText().toString();
				String wsdUrl="http://www.webxml.com.cn/webservices/weatherwebservice.asmx";
				String method="getWeatherbyCityName";
				Object result=SOAPUtil.doTransport(wsdUrl, method, cityname);
				results.setText(result.toString());
				
				
			}
		});
    }

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

    
}


最后别忘了,进行网络访问许可:

<!-- 允许访问网络 -->
    <uses-permission  android:name="android.permission.INTERNET"/>


效果如图显示:

如上图,结果显示中有许多我们并不希望显示出来的信息如anyType,String等都显示出来了,在原先的基础上修改一点代码即可实现自己希望的结果哦!

请看下一篇吐舌头奋斗

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值