android调用web service(cxf)实例应用详解

yizhihongxing

Android调用Web Service(CXF)实例应用详解

目录

前言

在 Android 开发中,需要调用 Web Service 的场景比较常见。Apache CXF 是一个开源的 Web Service 框架,支持多种协议,包括 SOAP、REST 和 XML/HTTP。本文将介绍使用 Apache CXF 调用 Web Service 的具体步骤,并提供两个示例,演示如何在 Android 应用中调用 Web Service。

步骤

在使用 Apache CXF 调用 Web Service 的过程中,需要完成以下几个步骤。

步骤一:创建 Web Service

在本文中,我们将使用 Eclipse 创建一个简单的 Web Service。具体步骤如下:

  1. 在 Eclipse 中选择“New Project”菜单,创建一个 Java Web 工程。
  2. 在 src 目录下创建一个 Java 类,命名为 WeatherService。
  3. 在 WeatherService 类中添加一个方法,用于获取天气信息。

java
public String getWeatherInfo(String city) {
String result = "";
try {
URL url = new URL("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx");
QName qname = new QName("http://WebXml.com.cn/", "WeatherWebServiceSoap");
Service service = Service.create(url, qname);
WeatherWebServiceSoap soap = service.getPort(WeatherWebServiceSoap.class);
result = soap.getWeatherbyCityName(city);
} catch (MalformedURLException e) {
e.printStackTrace();
}
return result;
}

  1. 在 WeatherService 类上右键,选择“Web Services”->“Create Web Service”。
  2. 在“Create Web Service”向导中,选择“Bottom Up Java Web Service”类型,指定 WeatherService 类为服务实现类。
  3. 点击“Next”继续,选择“Apache CXF 2.x”作为 Web Service 框架,点击“Finish”完成 Web Service 创建。

步骤二:创建 Android 工程

在 Android Studio 中创建一个空白项目。

步骤三:修改 AndroidManifest.xml 文件

将以下权限添加到 AndroidManifest.xml 文件中:

<uses-permission android:name="android.permission.INTERNET" />

步骤四:导入 CXF 库文件

将以下库文件添加到 Android 工程中:

  • cxf-core-3.1.6.jar
  • cxf-rt-bindings-soap-3.1.6.jar
  • cxf-rt-frontend-jaxws-3.1.6.jar
  • cxf-rt-transports-http-3.1.6.jar

步骤五:创建 Web Service 的代理类

在 Android 工程中创建一个名为 WeatherServiceProxy 的类,并在该类中添加以下代码:

public class WeatherServiceProxy {
  private WeatherWebServiceSoap weatherWebService;

  public WeatherServiceProxy() {
    URL url = null;
    try {
      url = new URL("http://10.0.2.2:8080/WeatherService/services/WeatherService?wsdl");
      QName qname = new QName("http://WebXml.com.cn/", "WeatherWebServiceSoap");
      Service service = Service.create(url, qname);
      weatherWebService = service.getPort(WeatherWebServiceSoap.class);
    } catch (MalformedURLException e) {
      e.printStackTrace();
    }
  }

  public String getWeatherInfo(String city) {
    return weatherWebService.getWeatherbyCityName(city);
  }
}

其中,“http://10.0.2.2:8080/WeatherService/services/WeatherService?wsdl”是刚刚创建的 Web Service 的访问地址。

步骤六:调用 Web Service

在 MainActivity 中,添加以下代码:

public class MainActivity extends AppCompatActivity {

  private EditText cityEditText;
  private Button queryButton;
  private TextView resultTextView;

  private WeatherServiceProxy weatherServiceProxy;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    cityEditText = (EditText) findViewById(R.id.cityEditText);
    queryButton = (Button) findViewById(R.id.queryButton);
    resultTextView = (TextView) findViewById(R.id.resultTextView);

    weatherServiceProxy = new WeatherServiceProxy();

    queryButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        String city = cityEditText.getText().toString();
        String result = weatherServiceProxy.getWeatherInfo(city);
        resultTextView.setText(result);
      }
    });
  }
}

其中,“http://10.0.2.2:8080/WeatherService/services/WeatherService?wsdl”是刚刚创建的 Web Service 的访问地址。

至此,使用 Apache CXF 调用 Web Service 就完成了。

示例说明

下面将提供两个示例,演示如何在 Android 应用中调用 Web Service。

示例 1:获取天气信息

Web Service 实现

参考步骤一中的步骤创建 WeatherService。

Android 工程实现

在 MainActivity 中,添加以下代码:

public class MainActivity extends AppCompatActivity {

  private EditText cityEditText;
  private Button queryButton;
  private TextView resultTextView;

  private WeatherServiceProxy weatherServiceProxy;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    cityEditText = (EditText) findViewById(R.id.cityEditText);
    queryButton = (Button) findViewById(R.id.queryButton);
    resultTextView = (TextView) findViewById(R.id.resultTextView);

    weatherServiceProxy = new WeatherServiceProxy();

    queryButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        String city = cityEditText.getText().toString();
        String result = weatherServiceProxy.getWeatherInfo(city);
        resultTextView.setText(result);
      }
    });
  }
}

其中,“WeatherServiceProxy”是刚刚创建的代理类。

示例 2:获取汽车信息

Web Service 实现

  1. 在 src 目录下创建一个 Java 类,命名为 CarService。
  2. 在 CarService 类中添加一个方法,用于获取汽车信息。

java
public String getCarInfo(String code) {
String result = "";
try {
URL url = new URL("http://www.webxml.com.cn/WebServices/CarInfoWebService.asmx");
QName qname = new QName("http://tempuri.org/", "CarInfoWebServiceSoap");
Service service = Service.create(url, qname);
CarInfoWebServiceSoap soap = service.getPort(CarInfoWebServiceSoap.class);
result = soap.getCarInfo(code, "");
} catch (MalformedURLException e) {
e.printStackTrace();
}
return result;
}

  1. 在 CarService 类上右键,选择“Web Services”->“Create Web Service”。
  2. 在“Create Web Service”向导中,选择“Bottom Up Java Web Service”类型,指定 CarService 类为服务实现类。
  3. 点击“Next”继续,选择“Apache CXF 2.x”作为 Web Service 框架,点击“Finish”完成 Web Service 创建。

Android 工程实现

在 MainActivity 中,添加以下代码:

public class MainActivity extends AppCompatActivity {

  private EditText codeEditText;
  private Button queryButton;
  private TextView resultTextView;

  private CarServiceProxy carServiceProxy;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    codeEditText = (EditText) findViewById(R.id.codeEditText);
    queryButton = (Button) findViewById(R.id.queryButton);
    resultTextView = (TextView) findViewById(R.id.resultTextView);

    carServiceProxy = new CarServiceProxy();

    queryButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        String city = codeEditText.getText().toString();
        String result = carServiceProxy.getCarInfo(code);
        resultTextView.setText(result);
      }
    });
  }
}

其中,“CarServiceProxy”是刚刚创建的代理类。

总结

本文介绍了使用 Apache CXF 调用 Web Service 的具体步骤,并提供了两个示例。希望对初学者有所帮助。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:android调用web service(cxf)实例应用详解 - Python技术站

(0)
上一篇 2023年6月27日
下一篇 2023年6月27日

相关文章

  • python使用OS模块操作系统接口及常用功能详解

    Python使用OS模块操作系统接口及常用功能详解 简介 Python的os模块提供了一种与操作系统进行交互的接口,可以执行各种操作系统相关的任务,如文件和目录操作、进程管理等。本攻略将详细介绍os模块的常用功能和使用方法。 文件和目录操作 获取当前工作目录 使用os.getcwd()函数可以获取当前工作目录的路径。 示例代码: import os curr…

    other 2023年8月8日
    00
  • C语言双向链表的原理与使用操作

    C语言双向链表的原理与使用操作 什么是双向链表 双向链表是由一系列结点组成的数据结构,每个结点除了有指向下一个结点的指针,还有指向上一个结点的指针。这种链表可以从头到尾或者从尾到头进行遍历。 双向链表的结构 下面是一个双向链表的结构体定义: typedef struct Node{ int data; struct Node *pre; struct Nod…

    other 2023年6月27日
    00
  • laravel5.5跨域问题解决方案

    当然,我很乐意为您提供有关“Laravel 5.5跨域问题解决方案”的完整攻略。以下是详细的步骤和两个示例: 1 跨域问题 跨域是指在浏览器中,当一个网页向不同的域名、端口或协议发送请求时,浏览器会阻止该请求。这是由于浏览器的同源策略所导致的。 2 Laravel 5.5跨域问题解决方案 Laravel 5.5提供了一种简单的方法来解决跨域问题。以下是解决跨…

    other 2023年5月6日
    00
  • Linux常用命令之grep命令用法详解

    Linux常用命令之grep命令用法详解 简介 grep 是一个强大的文本搜索工具,用于在文件中查找指定的模式。它可以根据用户提供的正则表达式进行匹配,并输出匹配到的行。 基本用法 grep 命令的基本语法如下: grep [选项] 模式 [文件…] 其中,选项 可以是以下常用选项之一:- -i:忽略大小写进行匹配。- -v:反向匹配,输出不包含模式的行…

    other 2023年8月19日
    00
  • Iptables防火墙自定义链表实现方式

    Iptables是一个非常强大的Linux防火墙工具,可以在网络层次上实现数据包的过滤与转发,保护网络安全。自定义链表是Iptables的一大特性,可以将复杂的规则划分到逻辑分组,提高规则的可读性和维护性。Iptables中默认包含了多个链表,如INPUT、OUTPUT、FORWARD等,我们可以在这些链表中自定义新的链表,也可以将自定义的链表插入到默认链表…

    other 2023年6月27日
    00
  • mysql字符串函数详细汇总

    MySQL 字符串函数详细汇总 MySQL 字符串函数是对字符串的操作函数。这些函数可以用来提取、操作和转换字符串的值。本文将会介绍 MySQL 数据库中可用的一些常见字符串函数。 CONCAT CONCAT() 函数用于将两个或多个字符串连接起来。它将参数作为字符串连接到一起。 示例: SELECT CONCAT(‘Hello’, ‘ World’); 输…

    other 2023年6月20日
    00
  • 初窥Linux 之我最常用的20条命令总结

    下面我来详细讲解一下“初窥Linux 之我最常用的20条命令总结”的完整攻略。 登录Linux系统 在终端输入ssh [用户]@[主机名]即可登录Linux系统,其中[用户]是你的用户名,[主机名]是你要连接的主机名或IP地址。 示例: ssh username@192.168.1.10 创建文件夹 使用mkdir命令可以创建一个新的文件夹,例如: mkdi…

    other 2023年6月26日
    00
  • Mysql树形递归查询的实现方法

    MySQL树形递归查询是指在MySQL数据库中,通过递归查询的方式获取树形结构数据的方法。该方法可以用于实现维度表、组织架构表等具有树形结构的数据的查询。 以下是实现MySQL树形递归查询的具体步骤: 一、创建示例表 创建一张示例数据表,包含ID、名称、父ID等字段。例如: CREATE TABLE `category` ( `id` int(10) uns…

    other 2023年6月27日
    00
合作推广
合作推广
分享本页
返回顶部