Android 请求WebService接口(SOAP协议)

    xiaoxiao2021-03-25  67

    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.asmxSOAPAction:http://webxml.com.cn/toTraditionalChineseMethodName:toTraditionalChineseParams: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错误,用起来会比较方便。

    转载请注明原文地址: https://ju.6miu.com/read-37899.html

    最新回复(0)