Android利用SOAP进行网络编程(2)

<soapenv:Envelope

xmlns:soapenv=“http:// schemas.xmlsoap.org/soap/envelope/”

xmlns:xsd=“http:// www.w3.org/2001/XMLSchema”

xmlns:xsi=“http:// www.w3.org/2001/XMLSchema-instance”>

soapenv:Body

<req:echo xmlns:req=“http:// localhost:8080/axis2/services/MySer-vice/”> req:categoryclassifieds</req:category>

</req:echo>

</soapenv:Body>

</soapenv:Envelope>

响应时候发送的消息:

<soapenv:Envelope

xmlns:soapenv=“http:// schemas.xmlsoap.org/soap/envelope/”

xmlns:wsa=“http:// schemas.xmlsoap.org/ws/2004/08/addressing”>

soapenv:Header

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 METHOD_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 showWeatherAsyncTask extends AsyncTask<String, Integer, String> {

@Override

protected String doInBackground(String… Urls) {

showWeather();

return null;

}

protected void onPostExecute(String result) {

}

};
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

如果你进阶的路上缺乏方向,可以加入我们的圈子和安卓开发者们一起学习交流!

  • Android进阶学习全套手册

    img

  • Android对标阿里P7学习视频

    img

  • BATJ大厂Android高频面试题

    img

最后,借用我最喜欢的乔布斯语录,作为本文的结尾:

人这一辈子没法做太多的事情,所以每一件都要做得精彩绝伦。
你的时间有限,所以不要为别人而活。不要被教条所限,不要活在别人的观念里。不要让别人的意见左右自己内心的声音。
最重要的是,勇敢的去追随自己的心灵和直觉,只有自己的心灵和直觉才知道你自己的真实想法,其他一切都是次要。

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

[外链图片转存中…(img-1k3w4bxj-1713792743518)]

  • BATJ大厂Android高频面试题

    [外链图片转存中…(img-qTqYaFlA-1713792743520)]

最后,借用我最喜欢的乔布斯语录,作为本文的结尾:

人这一辈子没法做太多的事情,所以每一件都要做得精彩绝伦。
你的时间有限,所以不要为别人而活。不要被教条所限,不要活在别人的观念里。不要让别人的意见左右自己内心的声音。
最重要的是,勇敢的去追随自己的心灵和直觉,只有自己的心灵和直觉才知道你自己的真实想法,其他一切都是次要。

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值