android 使用Ksoap2工具类实现WebService网络编程
1.下载Ksoap2,将jar包拷贝到libs目录下。然后右键点击拷贝进来的jar,在弹出菜单中点击Add As Library.
2.在AndroidManifest.xml中添加访问网络的权限
- <uses-permission android:name="android.permission.INTERNET"/>
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.company.webservicedemo">
- <uses-permission android:name="android.permission.INTERNET"/>
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
- <activity
- android:name=".MainActivity"
- android:label="@string/app_name"
- android:theme="@style/AppTheme.NoActionBar">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
3.简单实现的布局
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- app:layout_behavior="@string/appbar_scrolling_view_behavior"
- tools:context="com.company.webservicedemo.MainActivity"
- tools:showIn="@layout/activity_main">
- <EditText
- android:id="@+id/orgCode_txt"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="请输入要查询的组织编码" />
- <Button
- android:id="@+id/get_btn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="获取组织信息" />
- <TextView
- android:id="@+id/show_txt"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- </LinearLayout>
4.自己实现的一个简单的接口。
5.点击获取按钮,调用接口,将返回的结果显示在TextView空间中。
- package com.company.webservicedemo;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- public class MainActivity extends AppCompatActivity {
- Button button;
- TextView textView;
- EditText orgCodeTxt;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- button = (Button) findViewById(R.id.get_btn);
- textView = (TextView) findViewById(R.id.show_txt);
- orgCodeTxt = (EditText) findViewById(R.id.orgCode_txt);
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- WebServiceUtils.getOrgInfo(orgCodeTxt.getText().toString(), new WebServiceUtils.CallBack() {
- @Override
- public void result(String result) {
- textView.setText(result);
- }
- });
- }
- });
- }
- }
6.调用WebService接口的具体实现,UI主线程中不能直接进行网络通信,需要在子线程中完成。返回的信息是在子线程中,需要利用Handler来实现子线程与主线程信息的传递。
- package com.company.webservicedemo;
- import android.os.Handler;
- import android.os.Message;
- import org.ksoap2.SoapEnvelope;
- import org.ksoap2.SoapFault;
- import org.ksoap2.serialization.SoapObject;
- import org.ksoap2.serialization.SoapSerializationEnvelope;
- import org.ksoap2.transport.HttpTransportSE;
- public class WebServiceUtils {
- public interface CallBack {
- void result(String result);
- }
- public static void getOrgInfo(final String orgCode, final CallBack callBack) {
- // 用于子线程与主线程通信的Handler
- final Handler mHandler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- super.handleMessage(msg);
- // 将返回值回调到callBack的参数中
- callBack.result((String) msg.obj);
- }
- };
- new Thread(new Runnable() {
- @Override
- public void run() {
- // 命名空间
- String nameSpace = "http://tempuri.org/";
- // 调用的方法名称
- String methodName = "GetOrgInfo";
- // EndPoint
- String endPoint = "http://192.168.1.12:8037/IWMSService.asmx?wsdl";
- // SOAP Action
- final String soapAction = "http://tempuri.org/GetOrgInfo";//nameSpace+methodName
- // 建立webservice连接对象
- final HttpTransportSE transport = new HttpTransportSE(endPoint);
- //transport.debug = true;// 是否是调试模式
- // 设置连接参数
- SoapObject soapObject = new SoapObject(nameSpace, methodName);
- soapObject.addProperty("orgCode", orgCode);
- // 设置返回参数
- final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
- SoapEnvelope.VER11);// soap协议版本必须用SoapEnvelope.VER11(Soap V1.1)
- envelope.dotNet = true;// 注意:这个属性是对dotnetwebservice协议的支持,如果dotnet的webservice需要设置成true
- envelope.bodyOut = soapObject;//千万注意!!
- envelope.setOutputSoapObject(soapObject);// 设置请求参数
- try {
- transport.call(soapAction, envelope);// 调用WebService
- } catch (Exception e) {
- mHandler.sendMessage(mHandler.obtainMessage(-1, e.getMessage()));
- }
- if (envelope.bodyIn instanceof SoapFault) {
- SoapFault error = (SoapFault) envelope.bodyIn;
- // 将异常的消息利用Handler发送到主线程
- mHandler.sendMessage(mHandler.obtainMessage(0, error.toString()));
- } else {
- SoapObject object = (SoapObject) envelope.bodyIn;// 获取返回的数据
- // 将结果利用Handler发送到主线程
- mHandler.sendMessage(mHandler.obtainMessage(1, object.getProperty(0).toString()));
- }
- }
- }).start();
- }
- }
android 使用Ksoap2工具类实现WebService网络编程的更多相关文章
- (转载)实例详解Android快速开发工具类总结
实例详解Android快速开发工具类总结 作者:LiJinlun 字体:[增加 减小] 类型:转载 时间:2016-01-24我要评论 这篇文章主要介绍了实例详解Android快速开发工具类总结的相关 ...
- Android 软件管理工具类Utils
Android 软件管理工具类Utils /** * Created by uilubo on 2015/9/30. * 工具类 */ public class Utils { public stat ...
- 一个使用命令行编译Android项目的工具类
一个使用命令行编译Android项目的工具类 简单介绍 编译apk项目须要使用的几个工具,基本都在sdk中,它们各自是(Windows系统): 1.aapt.exe 资源打包工具 2.android. ...
- Android开发常用工具类
来源于http://www.open-open.com/lib/view/open1416535785398.html 主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java. 目前 ...
- Android常用的工具类
主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java.目前包括HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils. Prefe ...
- android 开发 常用工具类
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38965311,本文出自[张鸿洋的博客] 打开大家手上的项目,基本都会有一大批的辅 ...
- Android常用的工具类(转)
主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java.目前包括HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils.Prefer ...
- 2013最新Android常用的工具类整理
主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java. 目前包括HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils. Pref ...
- 最全Android开发常用工具类
主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java. 目前包括 HttpUtils.DownloadManagerPro.Safe.ijiami.ShellUtils.Pack ...
随机推荐
- C#自动实现Dll(OCX)控件注册的两种方法
尽管MS为我们提供了丰富的.net framework库,我们的程序C#开发带来了极大的便利,但是有时候,一些特定功能的控件库还是需要由第三方提供或是自己编写.当需要用到Dll引用的时候,我们通常会通 ...
- Mysql Explain 解读(基于MySQL 5.6.36)
Mysql Explain 解读(基于MySQL 5.6.36) 1.语法 explain < table_name > #例子 explain select * from t3 wher ...
- sublime text3添加右键打开的操作
前一段重新安装了Sublime Text3,不过一直不在右键菜单中,所以决定添加,有如下2种方法. 方法一(推荐). 把以下代码,复制到SublimeText3的安装目录,然后重命名为:sublime ...
- Python之mysql数据库更新表数据接口实现
昨天,因为项目需求要添加表的更新接口,来存储预测模型训练的数据. 先码为敬~~~~~~~ # -*- coding: utf-8 -*- import pymysql import settings ...
- jq 时间计算
-(function($){ var caculation = function(gap){ var minutes = 1000 * 60 var hours = minutes * 60 var ...
- Java 读取 .properties 配置文件的几种方式
Java 开发中,需要将一些易变的配置参数放置再 XML 配置文件或者 properties 配置文件中.然而 XML 配置文件需要通过 DOM 或 SAX 方式解析,而读取 properties 配 ...
- 鼠标相关操作(Cursor类及相关API)
Cursor.visible:属性,显示或者隐藏鼠标. Cursor.lockState = CursorLockMode.Locked:锁定鼠标到游戏窗口的中心. (CursorLockMode: ...
- react入门到进阶(一)
一.何为react Facebook在F8会议上首次提出这个概念,一套全新的框架就此诞生. React 不是一个完整的 MVC.MVVM 框架,其只负责 View 层 React 跟 Web Comp ...
- 模板文件引入css样式文件
引用路径问题:相对路径和绝对路径 相对路径:相对路口文件index.php设置 绝对路径:从虚拟主机站点目录开始设置 css样式文件引入图片,路径的设置 相对地址:相对css文件本身设置 ①模板文件 ...
- vue.js权威指南 PDF
链接:https://pan.baidu.com/s/1c2ItN6S 密码:ya8r