Android调用WebService

  • 3.WebService的模型

  • Android调用WebService的简单实现

    • 1.去哪里获取WebService
  • 2.第三方jar包的准备

  • 3.获取相关参数

  • 4.导入jar包

  • 5.布局文件如下:

  • 6.实现代码如下:

  • 7.实现结果如下:

前言

=============================================================

笔者本学期学习了关于WebService的相关知识,对Duddo和SpringCloud也有所学习,不过学习的都是在java平台上调用WebService服务,这学期接触了Android开发,遂想在Android平台上调用WebService,本篇文章会简单介绍WebService和在Android平台上简单调用WebSerive实例。

WebService简介

=======================================================================

1.WebSerive的引入


(1)为什么需要WebService?

我们都知道电脑,手机硬件资源是有限的,对于一些复杂的数据处理,通过自己的硬件资源肯定是不够的,但是我们又想获取服务,这时候我们就可以通过调用远程服务器的服务来满足我们的需求,这样我们就不需要关心服务相关的实现,只要学会如何调用。

(2)什么是WebService?

简单的说就是某些站点开放出来的服务,当然你也可以自己开发一个service,也就是一 些方法 ,通过URL,指定某一个方法名,发出请求,站点的这个服务(方法) ,接收请求后,根据传入的参数做一些处理 ,然后将处理后的结果以XML形式返回给你,你的程序就解析这些XML参数,然后显示出来或做其他操作。

例如:很多大的站点提供有天气预报的webservice.查询某网站的数据的webservice ,只要你发送请求过来,它就返回天气预报、某网站的数据,然后你把结果显示处来。

2.WebSerive相关技术


(1)XML

可扩展的标记语言。Xml是所有开发人员都熟悉的东西,不用多说,同时采用

XML Schema。正因为WebService采用 了Xml ,才使得它可以跨越各种编

程语言。

(2)SOAP

简单对象访问协议。提供了标准的RPC方法来调用WebService。SOAP规范

中定义了SOAP消息格式,以及怎样通过HTTP协议来使用SOAP。SOAP是

基于XML语言和XSD标准的,其中XML是SOAP的数据编码方式。

(3)WSDL

WSDL是一种基于XML的用于描述WebService及其操作、 参数和返回值得

语言。因为是基于XML ,一些IDE可以根据WebService来生成WSDL文档,

又能通过导入WSDL文档,生成调用相应的WebService代码。

(4)UDDI

UDDI用于在网上自动查找WebService ,一旦WebService注册到UDDI ,

客户就可以很方便的查找和定位所需要的WebService.

3.WebService的模型


服务提供者:把自己的服务注册到"服务注册中心”

服务请求者:到"服务注册中心”查找到相应的服务,然后定位到“ 服务提供者”

服务提供者:开始为“服务请求者”提供相应的服务

Android调用WebService的简单实现

===================================================================================

1.去哪里获取WebService


网上有很多提供WebService的站点,首先找到这些站点,然后获取相应的服务即可!

这里选取WebXml作为此次实例进行讲解,下面是他们的官网:

http://www.webxml.com.cn/zh_cn/index.aspx

webXml的相关页面:

在这里插入图片描述

2.第三方jar包的准备


首先如果想在Android平台上调用WebService需要依赖于第三方类库:ksoap2

而在Android平台上,使用的是ksoap2 Android,一个高效,轻量级的SOAP开发包!

jar包下载地址:http://simpligility.github.io/ksoap2-android/getting-started.html

3.获取相关参数


首先找到我们需要获取的服务,然后记录相关的参数:NameSpace(命名空间),SoapAction以及URL就不用说了,其他参数这样找:

此次实例将调用国内手机号码归属地查询WEB服务

首先点击下面的链接进入参数文档

http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx

参数文档如下:

在这里插入图片描述

点击getMoblieCodeInfo,进入页面,将页面中的SoapAction,NameSpace以及相关参数mark下!

在这里插入图片描述

4.导入jar包


在这里插入图片描述

5.布局文件如下:


<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:padding=“5dp”

tools:context=“.MainActivity”>

<EditText

android:id=“@+id/edit_param”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:hint=“请输入查询的相关参数” />

<Button

android:id=“@+id/btn_attribution”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“号码归属地查询” />

<TextView

android:id=“@+id/txt_result”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_marginLeft=“5dp”

android:layout_marginTop=“5dp”

android:text=“结果显示:”

android:textSize=“16sp” />

6.实现代码如下:


package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.Toast;

import org.ksoap2.SoapEnvelope;

import org.ksoap2.serialization.SoapObject;

import org.ksoap2.serialization.SoapSerializationEnvelope;

import org.ksoap2.transport.HttpTransportSE;

public class MainActivity extends AppCompatActivity {

private EditText edit_param;

private Button btn_attribution;

private TextView txt_result;

private String result;

//定义获取手机信息的SoapAction与命名空间,作为常量

private static final String AddressnameSpace = “http://WebXml.com.cn/”;

//归属地查询相关参数

private static final String Addressurl = “http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx”;

private static final String Addressmethod = “getMobileCodeInfo”;

private static final String AddresssoapAction = “http://WebXml.com.cn/getMobileCodeInfo”;

//定义一个Handler用来更新页面:

private Handler handler = new Handler() {

public void handleMessage(Message msg) {

switch (msg.what) {

case 0x002:

txt_result.setText(“结果显示:\n” + result);

Toast.makeText(MainActivity.this, “号码归属地查询成功”, Toast.LENGTH_SHORT).show();

break;

}

}

};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

bindViews();

}

private void bindViews() {

edit_param = (EditText) findViewById(R.id.edit_param);

btn_attribution = (Button) findViewById(R.id.btn_attribution);

txt_result = (TextView) findViewById(R.id.txt_result);

btn_attribution.setOnClickListener(this);

}

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.btn_attribution:

new Thread(new Runnable() {

public void run() {

getland();

}

}).start();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值