阶段一:进行主界面布局,如下图:

具体代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/ba">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/input_city" />
<EditText
android:id="@+id/city"
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="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
阶段二:导入KSOAP包.
将ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar包拷贝到libs文件夹下,然后点击右键Build Path –> Configure Build Path...,会出现

然后将添加的包勾上...
阶段三:调用天气预报的WebService
1.指定 WebService 的命名空间和调用方法
import org.ksoap2.serialization.SoapObject;
private static final String NAMESPACE = "http://WebXml.com.cn/";
private static final String METHOD_NAME = "getWeatherbyCityName";
SoapObject soapObject = new SoapObject(NAMESPACE, METHOD_NAME);
SoapObject类的第一个参数表示WebService的命名空间,可以从WSDL文档中找到WebService的命名空间。
第二个参数表示要调用的WebService方法名。
2、设置调用方法的参数值,如果没有参数,可以省略,设置方法的参数值的代码如下:
rpc.addProperty("theCityName", "济宁");
要注意的是,addProperty方法的第1个参数虽然表示调用方法的参数名,但该参数值并不一定与服务端的WebService类中的方法参数名一致,只要设置参数的顺序一致即可。
3、生成调用Webservice方法的SOAP请求信息。
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = soapObject;
envelope.dotNet = true;
envelope.setOutputSoapObject(soapObject);
创建SoapSerializationEnvelope对象时需要通过SoapSerializationEnvelope类的构造方法设置SOAP协议的版本号。
该版本号需要根据服务端WebService的版本号设置。
在创建SoapSerializationEnvelope对象后,不要忘了设置SOAPSoapSerializationEnvelope类的bodyOut属性,
该属性的值就是在第一步创建的SoapObject对象。
4、创建HttpTransportsSE对象。
这里不要使用 AndroidHttpTransport ht = new AndroidHttpTransport(URL); 这是一个要过期的类
private static String wsdlUrl = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx";
HttpTransportSE ht = new HttpTransportSE(wsdlUrl);
ht.debug = true;
5、使用call方法调用WebService方法
private static String SOAP_ACTION = "http://WebXml.com.cn/getWeatherbyCityName";
ht.call(SOAP_ACTION, envelope);
网上有人说这里的call的第一个参数为null,但是经过我的测试,null是不行的。
第2个参数就是在第3步创建的SoapSerializationEnvelope对象。
6、获得WebService方法的返回结果
有两种方法:
a.使用getResponse方法获得返回数据。
private SoapObject detail;
detail =(SoapObject) envelope.getResponse();
b.使用 bodyIn 及 getProperty。
private SoapObject detail;
SoapObject result = (SoapObject)envelope.bodyIn;
detail = (SoapObject) result.getProperty("getWeatherbyCityNameResult");
7、 这时候执行会出错,提示没有权限访问网络
需要修改 AndroidManifest.xml 文件,赋予相应权限
简单来说就是增加下面这行配置:<uses-permission android:name="android.permission.INTERNET"></uses-permission>
关于SOAPUtil类的完整代码如下:
package com.lks.weathersearch.soap;
import java.io.IOException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
public class SOAPUtil {
public static Object doTransport(final String wsdlUrl,
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:"+city);
SoapSerializationEnvelope serializationEnvelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
serializationEnvelope.bodyOut = soapObject;
serializationEnvelope.dotNet = true;
serializationEnvelope.setOutputSoapObject(soapObject);
HttpTransportSE httpTransportSE = new HttpTransportSE(wsdlUrl);
String SOAP_ACTION = "http://WebXml.com.cn/" + webMethod;
System.out.println("action:"+SOAP_ACTION);
try {
httpTransportSE.call(SOAP_ACTION, serializationEnvelope);
if (serializationEnvelope.getResponse() != null) {
SoapObject result = (SoapObject) serializationEnvelope.getResponse();
System.out.println(result);
return result;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
阶段四:编写WeatherActivity,进行相应的事件处理.具体代码如下:
package com.lks.weathersearch.web;
import org.ksoap2.serialization.SoapObject;
import com.lks.weathersearch.soap.SOAPUtil;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
import android.text.Editable;
public class WeatherActivity extends Activity {
EditText cityText;
Button searchButton;
TextView resultView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_weather);
cityText = (EditText) this.findViewById(R.id.city);
searchButton = (Button) this.findViewById(R.id.search);
resultView = (TextView) this.findViewById(R.id.result);
searchButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String city = cityText.getText().toString();
String wsdlUrl = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx";
String webMethod = "getWeatherbyCityName";
SoapObject result = (SoapObject) SOAPUtil.doTransport(wsdlUrl, webMethod, city);
resultView.setText(result.getProperty(4).toString()+"\n"+result.getProperty(10).toString());
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_weather, menu);
return true;
}
}
运行结果显示:

相应的项目已上传到资源~欢迎下载~
分享到:
相关推荐
本实例将深入探讨Android中如何使用Webservice,特别是针对天气预报服务的调用。我们将关注以下几个核心知识点: 1. **Web Service基础**:Web Service是一种基于互联网的、标准化的、能够跨平台进行通信的软件服务...
本文将详细介绍 Android 调用天气预报的 WebService 的简单例子,包括获取并使用 KSOAP 包、调用 WebService 的步骤等。 一、获取并使用 KSOAP 包 在 Android SDK 中并没有提供调用 WebService 的库,因此,需要...
在Android开发中,调用外部Web服务,如天气预报API,是常见的功能需求。本教程将专注于如何在Android应用中实现这一功能,特别是在这个名为"WeatherSearch"的项目中。我们将探讨以下几个关键知识点: 1. **Android...
【Android天气预报Webservice应用程序】 在移动应用开发中,Android平台提供了一个强大的框架来构建功能丰富的应用程序。本项目“android天气预报之Webservice应用程序”是一个典型的示例,它展示了如何利用Android...
在这个“android 天气预报实例”中,我们将深入探讨如何利用Android SDK调用WebService接口来获取并显示天气预报信息。 首先,我们要了解的是Android中的网络编程。在Android系统中,网络操作通常在后台线程执行,...
【Android与WebService天气预报Demo详解】 在移动应用开发中,实时获取天气信息是常见的功能之一。本Demo结合了Android客户端和WebService技术,为用户展示如何从远程服务器获取并展示天气预报数据。通过这个实例,...
本实例"android天气预报Demo"就是这样一个完整的实践项目,通过集成Webservice接口来获取全国范围内的天气信息,并在Android设备上进行展示。 首先,我们要了解如何使用Webservice。在本例中,开发者可能使用了HTTP...
在Android开发中,调用Web Service是常见的数据交互方式,特别是在获取远程服务器上的实时信息,如天气预报时。本文将详细讲解如何在Android应用中利用Webservice获取天气数据,并以"android下访问webservice服务...
通过一个登录的案例,重点演示了android如何调用服务端的webservice,都采用了apache的框架, 服务端接口的返回数据使用了压缩加密的技术,10K的数据在压缩加密之后变成1K,这是手机省流量的关键技术所在,android...
在Android开发中,调用Web ...总之,Android调用Web Service获取天气预报涉及网络通信、数据解析、异常处理、UI设计等多个方面,通过合理的技术选型和规范的编程实践,可以构建出高效、稳定且用户体验良好的应用。
### Android调用.NET WebService详解 #### 一、概述 在Android开发中,与服务器进行交互是必不可少的一个环节。常见的交互方式包括HTTP请求、RESTful API等,而WebService作为一种传统但依然广泛使用的通信协议,...
使用ksoap2调用Webservice实例 需将lib文件夹设置为源文件夹 在AndroidManifest.xml文件中加入<uses-permission android:name="android.permission.INTERNET" />
Android调用WebService的例子(包括服务器端和客户端)。服务器端使用CXF框架发布,Android客户端使用KSOAP2包读取数据。 分别提供了JavaBean、XML、JSON数据类型的通信方式。
以下是一个简单的调用Webservice的例子: ```java SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); request.addProperty("param1", value1); request.addProperty("param2", value2); ...
Android通过webservice连接SqlServer实例(Android4.0可运行)从原博http://blog.csdn.net/zhyl8157121/article/details/8169172下载下来的demo,看了评论和其它文章后修改,解决了Android2.3以上StrictMode模式下不...
**二、Android调用Web Service的步骤** 1. **构建服务端:** - 使用Java(J2EE)编写Web Service接口和实现,比如使用Java的JAX-WS(Java API for XML Web Services)。 - 配置并部署服务到Web服务器,如Tomcat,...
1. 调用WebService:使用Android的网络API(如HttpURLConnection)或者Retrofit等库,构造请求URL,携带必要的参数(如SQL查询语句),向服务器发送请求。 2. 数据解析:收到服务器返回的XML或JSON响应后,使用如DOM...
【标题】:“webService调用实例 Demo” 在IT行业中,Web Service是一种常见的应用程序接口(API)形式,它允许不同系统间的交互,无论它们是用何种编程语言或运行在何种操作系统上。本“webService调用实例 Demo”...
2. **Android调用Web服务** - **HTTP请求库**:Android客户端需要一个库来发送HTTP请求并解析响应。常见的库有HttpURLConnection(内置)、Volley、OkHttp和Retrofit等。这里我们以Retrofit为例,它是一个强大的...