wsa:ReplyTo
wsa:Addresshttp:// schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:Address> </wsa:ReplyTo>
wsa:Fromwsa:Addresshttp:// localhost:8080/axis2/services/MyService</wsa:Address> </wsa:From>
wsa:MessageIDECE5B3F187F29D28BC11433905662036</wsa:MessageID>
</soapenv:Header>
soapenv:Body
<req:echo xmlns:req=“http:// localhost:8080/axis2/services/MyService/”>
req:categoryclassifieds</req:category>
</req:echo>
</soapenv:Body>
</soapenv:Envelope>
5.SOAP 调用 WebService的步骤
(1)添加ksoap2 包,适合手机的 WebService 客户端的 SDK 有一些,比较常用的是 Ksoap2, 可以从网址http://code.google.com/p/ksoap2-android/ 下载,然后将下载的ksoap2-androidassembly-2.4-jar-with-dependencies.jar 包复制到Eclipse 工程的lib 目录中,当然也可以放在 其他的目录里。在 Eclipse 工程中引用这个 jar 包。 下图是WebService的主页面。
(2)指定 WebService 的命名空间和调用的方法名,SoapObject 类的第一个参数表示WebService 的命名空间,可以从WSDL 文档中找到 WebService 的命名空间;第二个参数表示要调用的 WebService 方法名。例如:
SoapObject request =new SoapObject(http:// service,“getName”);
(3)设置调用方法的参数值,如果没有参数,可以省略。例如:
Request.addProperty(“nameone”,“value”);
Request.addProperty(“nametwo”,“value”);
(4)生成调用WebService 方法的SOAP 请求信息。信息由SoapSerializationEnvelope 对象描述。
SoapSerializationEnvelope envelope=
new SoapSerializationEnvelope(SoapEnvelope.VER11);
Envelope.bodyOut =
request;
(5)创建HttpTransportsSE 对象。
HttpTransportSE ht=new HttpTransportSE
(“http:// fy.webxml.com.cn/webservices/EnglishChinese.asmx?wsdl”);
(6)使用 call 方法调用 WebService 方法。
ht.call(null,envelope);
(7)使用 getResponse 方法获得 WebService 方法的返回结果并解析返回内容。
SoapObject soapObject =(SoapObject)envelope.getResponse();
6.利用SOAP实现天气服务的解析
(1)具体实现过程:从客户端获取用户输入的城市名称,将城市名称打包成符合SOAP 协议的查询消息,把查询信息发送给提供SOAP 天气服务的服务器 ;服务器内部进行操作之后,返回给客户端查询城市的天气信息,该信息以SOAP 格式返回,客户端对其进行解析之后显示给用户。
(2)具体操作:用户在文本框中输入城市名之后单击“查询”按钮,查询成功后,会在应用界面上显示所查询城市的天气信息。
(3)先编写布局文件中的控件
显示控件,用于显示天气情况 :
<TextView
android:id=“@+id/textView1”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_centerHorizontal=“true”
android:layout_centerVertical=“true”
android:padding=“@dimen/padding_medium”
tools:context=“.AndroidSoapActivity” />
输入控件,用户输入城市名称:
<EditText
android:id=“@+id/cityName”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_alignParentLeft=“true”
android:layout_alignParentTop=“true”
android:text=“@string/cityName” />
按钮,用户提交城市名称时候单击该按钮:
<Button
android:id=“@+id/ok”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_alignParentTop=“true”
android:layout_toRightOf=“@+id/textView1”
android:text=“@string/search” />
(4)完成应用内部对查询处理的主要代码:
import java.io.UnsupportedEncodingException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
SOAP方式查询天气情况包括:指定命名空间、给出接口地址、设置方法名、设置查询接口参数
public class AndroidSoapActivity extends Activity {
private static final String NAMESPACE = “http:// WebXml.com.cn/”;
private static String URL = “http://www.webxml.com.cn/webservices/weatherwebservice.asmx”; private static final String METH
资料获取→专栏
OD_NAME = “getWeatherbyCityName”;
private static String SOAP_ACTION = “http:// WebXml.com.cn/getWeatherbyC-ityName”;
private String weatherToday;
private Button okButton;
private SoapObject detail;
private EditText cityNameText;
private TextView cityMsgView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_soap);
cityNameText =(EditText)findViewById(R.id.cityName);
cityMsgView = (TextView)findViewById(R.id.textView1);
okButton = (Button) findViewById(R.id.ok);
okButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
new showWeatherAsyncTask().execute();
}
});
}
使用AsyncTask异步方式获取并显示天气信息
private class showWeatherAsyn
cTask extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String… Urls) {
showWeather();
return null;
}
protected void onPostExecute(String result) {
}
};
private void showWeather() {
String city = cityNameText.getText().toString().trim();
if(!city.isEmpty()){ getWeather(city);
}
}
public void getWeather(String cityName) {
try {
SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);
rpc.addProperty(“theCityName”, cityName);
HttpTransportSE ht = new HttpTransportSE(URL);
ht.debug = true;
SoapSerializationEnvelopeenvelope=
newSoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = rpc;
envelope.dotNet = true;
envelope.setOutputSoapObject(rpc);
ht.call(SOAP_ACTION, envelope);
SoapObject result = (SoapObject) envelope.bodyIn;
detail = (SoapObject)result.getProperty(“getWeatherbyCityNameResult”); System.out.println(“detail” + detail);
parseWeather(detail);
return;
}
catch (Exception e) {
e.printStackTrace();
}
}
解析SoapObject对象
private void parseWeather(SoapObject detail)throws UnsupportedEncodingException {
String date = detail.getProperty(6).toString();
weatherToday = “今天:” + date.split(" ")[0];
weatherToday = weatherToday + " 天气:" + date.split(" ")[1];
weatherToday = weatherToday + " 气温:" + detail.getProperty(5).toString() ;
weatherToday = weatherToday + " 风力:" + detail.getProperty(7).toString()+ " "; System.out.println("weatherToday is " + weatherToday);
cityMsgView.setText(weatherToday);
operty(6).toString();
weatherToday = “今天:” + date.split(" ")[0];
weatherToday = weatherToday + " 天气:" + date.split(" ")[1];
weatherToday = weatherToday + " 气温:" + detail.getProperty(5).toString() ;
weatherToday = weatherToday + " 风力:" + detail.getProperty(7).toString()+ " "; System.out.println("weatherToday is " + weatherToday);
cityMsgView.setText(weatherToday);