Android开发(二):调用WebService接口实现某些功能(Soap协议)

一:调用WebService接口需要进行网络访问,有几点要注意。
(1)在AndroidManifest.xml文件中添加以下代码:

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

(2)在Android中需要新建子线程进行网络连接。建立子线程:

 new Thread() {
            @Override
            public void run() {
                try {
                    //进行网络连接的代码
                } catch (Exception e) { e.printStackTrace(); }
            }
        }.start();

二:WebService接口用到SOAP协议。
(1)Soap需要下载Ksoap2-android的jar包,网上随便一搜就有。
把下载好的ksoap2的jar包复制到截图中的位置。
(链接:https://pan.baidu.com/s/1HhbdxMDjh1JB5G_4Bbm3DA
提取码:stjv )
在这里插入图片描述
然后在File->Project Structure->Dependencies,点击下图的+,选第二个。在这里插入图片描述
然后是这个界面,选择Ksoap2之后,一路OK就可以使用Soap了。在这里插入图片描述

(2)配置URL,命名空间,调用的方法和SoapAction等参数,然后封装Soap对象。这里以繁简转换的接口为例。

 String serviceURL="http://www.webxml.com.cn/WebServices/TraditionalSimplifiedWebService.asmx";
            String NameSpace="http://webxml.com.cn/";
            String methodName="toTraditionalChinese";
            final String soapAction="http://webxml.com.cn/toTraditionalChinese";
            SoapObject rpc=new SoapObject(NameSpace,methodName);

            TextView Name=findViewById(R.id.txtName);
            String strName=Name.getText().toString();

            rpc.addProperty("sText",strName);
            //rpc.addProperty("Pwd",txtPwd.getText().toString());
            final SoapSerializationEnvelope soapEnvelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
            soapEnvelope.dotNet=true;
            soapEnvelope.bodyOut=rpc;
            //发送Http请求
            final HttpTransportSE ht=new HttpTransportSE(serviceURL)
            new Thread() {
                @Override
                public void run() {
                    try {
                        ht.call(soapAction,soapEnvelope);
                        SoapObject object=(SoapObject)soapEnvelope.bodyIn;
                        String data = object.getProperty("toTraditionalChineseResult").toString();
                        Message msg=Message.obtain();
                        msg.obj=data;
                        mHandler.sendMessage(msg);
                    }catch (Exception e){e.printStackTrace();}
                }
            }.start();

三:下面贴出全部源代码
(1)效果图
在这里插入图片描述
(2)XML代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"

    tools:context=".MainActivity">

    <TextView
        android:id="@+id/show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.175"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toEndOf="@+id/button"
        app:layout_constraintTop_toTopOf="@+id/button"
        app:layout_constraintVertical_bias="0.541" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="152dp"
        android:text="转换"
        android:textSize="18sp"
        android:background="@drawable/shape_radius"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.306"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/txtName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="56dp"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

(3)MainActivity.java代码(这里使用Handler传递子线程的数据)

package com.example.test;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.Context;
import android.nfc.Tag;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpResponseException;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MainActivity extends AppCompatActivity {

    public Button btn;
    public TextView mTextView;
    public Handler mHandler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn=findViewById(R.id.button);
        btn.setOnClickListener(onClick);

        mTextView = (TextView) findViewById(R.id.show);
        // 步骤2:在主线程中创建Handler实例
        mHandler = new Mhandler();
    }

    public View.OnClickListener onClick=new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            // 采用继承Thread类实现多线程演示
            String serviceURL="http://www.webxml.com.cn/WebServices/TraditionalSimplifiedWebService.asmx";
            String NameSpace="http://webxml.com.cn/";
            String methodName="toTraditionalChinese";
            final String soapAction="http://webxml.com.cn/toTraditionalChinese";
            SoapObject rpc=new SoapObject(NameSpace,methodName);

            TextView Name=findViewById(R.id.txtName);
            String strName=Name.getText().toString();

            rpc.addProperty("sText",strName);
            //rpc.addProperty("Pwd",txtPwd.getText().toString());
            final SoapSerializationEnvelope soapEnvelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
            soapEnvelope.dotNet=true;
            soapEnvelope.bodyOut=rpc;
            //发送Http请求
            final HttpTransportSE ht=new HttpTransportSE(serviceURL);
            new Thread() {
                @Override
                public void run() {
                    try {
                        ht.call(soapAction,soapEnvelope);
                        SoapObject object=(SoapObject)soapEnvelope.bodyIn;
                        String data = object.getProperty("toTraditionalChineseResult").toString();
                        Message msg=Message.obtain();
                        msg.obj=data;
                        mHandler.sendMessage(msg);
                    }catch (Exception e){e.printStackTrace();}
                }
            }.start();
        }
    };
    class Mhandler extends Handler {
        // 通过复写handlerMessage() 从而确定更新UI的操作
        @Override
        public void handleMessage(Message msg) {
            mTextView.setText((String)msg.obj);
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ewind927

如果觉得有用,可以请我喝奶茶

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值