Android 请求WebService接口(SOAP协议)
来源:互联网 发布:无约束非线性优化问题 编辑:程序博客网 时间:2024/05/09 07:09
WebService简介
Web Services 是应用程序组件,使用开放协议进行通信,是独立的(self-contained)并可自我描述,可通过使用UDDI来发现,可被其他应用程序使用(跨平台性),XML 是 Web Services 的基础。
基础的WebService平台是XML+HTTP
WebService平台的元素
- SOAP (简易对象访问协议)
- UDDI (通用描述、发现及整合)
- WSDL (Web services 描述语言)
关于SOAP协议
SOAP 是基于 XML 的简易协议,可使应用程序在 HTTP 之上进行信息交换。
关于WebService更多介绍
1.下载Jar包(ksoap2-android)
因为Android是没有提供访问WebService接口的API的,所以我们需要下载Google提供的第三方Jar包导入到项目中。
Jar包地址
2.请求WebService接口,我们以(简体字转繁体字)为例子。
简体字转繁体字接口
请求体需要用到的参数如下:
- 命名空间:http://webxml.com.cn/
- URL:http://www.webxml.com.cn/WebServices/TraditionalSimplifiedWebService.asmx
- SOAPAction:http://webxml.com.cn/toTraditionalChinese
- MethodName:toTraditionalChinese
- Params:sText
响应体返回的数据:
- Key : toTraditionalChineseResult
3.开始请求WebService接口
- Manifest权限
<uses-permission android:name="android.permission.INTERNET"/>
- XML代码
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical"> <EditText android:id="@+id/edit_text" android:layout_width="match_parent" android:layout_height="wrap_content"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="转换" android:onClick="convert"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/text_result"/></LinearLayout>
- Java代码
package com.xuyongjun.webservice;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.support.v7.app.AppCompatActivity;import android.text.TextUtils;import android.view.View;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;import org.xmlpull.v1.XmlPullParserException;import java.io.IOException;public class MainActivity extends AppCompatActivity{ private EditText mEditText; private TextView mTextResult; private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { String text = (String) msg.obj; mTextResult.setText(text); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mEditText = (EditText) findViewById(R.id.edit_text); mTextResult = (TextView) findViewById(R.id.text_result); } public void convert(View view) { String text = mEditText.getText().toString(); if (TextUtils.isEmpty(text)) { Toast.makeText(this, "输入框不能为空", Toast.LENGTH_SHORT).show(); return; } requestWebService(text); } private void requestWebService(String text) { String url = "http://www.webxml.com.cn/WebServices/TraditionalSimplifiedWebService.asmx"; String nameSpace = "http://webxml.com.cn/"; final String soapAction = "http://webxml.com.cn/toTraditionalChinese"; String methodName = "toTraditionalChinese"; //封装Soap对象 SoapObject object = new SoapObject(nameSpace, methodName); object.addProperty("sText", text); //指定SOAP协议版本1.1 final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); //是否调用.net开发的接口 envelope.dotNet = true; //指定传入的参数 envelope.bodyOut = object; //Http请求对象 final HttpTransportSE httpTransportSE = new HttpTransportSE(url); new Thread() { @Override public void run() { try { //发送请求 httpTransportSE.call(soapAction,envelope); //拿到响应体 SoapObject response = (SoapObject) envelope.bodyIn; //取数据 String data = response.getProperty("toTraditionalChineseResult").toString(); Message msg = Message.obtain(); msg.obj = data; handler.sendMessage(msg); } catch (IOException e) { e.printStackTrace(); } catch (XmlPullParserException e) { e.printStackTrace(); } } }.start(); }}
- 效果图
关于上面代码并不是很完善,大家可以根据步骤将代码封装起来,比如处理Catch错误,用起来会比较方便。
0 0
- Android 请求WebService接口(SOAP协议)
- Android Soap请求webService
- Android WebService(基于SOAP协议)
- android soap协议与webservice
- jmeter发送soap请求-webservice接口
- Android基于SOAP协议向WebService交互数据,修改请求超时时间
- Android基于SOAP协议向WebService交互数据,修改请求超时时间
- Android基于SOAP协议向WebService交互数据,修改请求超时时间
- Android WebService(基于SOAP协议) 连接方法
- Retrofit2+Okhttp3+Rxjava2通过SOAP协议请求WebService
- jmeter发送soap请求进行webservice接口压力测试
- Jmeter发送SOAP请求对WebService接口测试
- Jmeter发送SOAP请求对WebService接口压力测试
- Jmeter3.2版本发送SOAP请求测试webservice接口
- Jmeter3.2版本发送SOAP请求测试webservice接口
- ios 进阶 -- 调用webservice接口(soap协议)的方法
- java 请求响应soap(webService)
- 抓取Webservice Soap请求
- JVM学习之类加载
- 产品新人必知的张小龙32条产品心经
- 冒泡、选择、插入、二分插入、希尔、堆、归并和基数排序算法小结
- 一些基础函数的笔记
- canvas关于Beginpath和Closepath的理解
- Android 请求WebService接口(SOAP协议)
- 03字符串操作和一级指针内存模型
- 多线程_同步代码快的锁及同步方法应用和锁的问题
- Web企业实训(java、tomcat、eclipse安装、环境配置及使用)
- [MIT 6.828] HW shell
- Java程序员开发参考资源
- 【程序员修炼日志】之初出茅庐的展望
- EffectiveJava(28)怎么利用有限制的通配符类型来提升API的灵活性
- 简单的数据库连接代码示范!!