• Android Studio 2.3.3 调用asp.net webService实战(拒绝忽悠)


    1、路径中不能包含localhost(本来想在本机调试,就是不行,没办法发布到远程服务器)

    2、必须采用异步的办法(阻塞主线程的是肯定不行了)

    3、以下是全部的源代码(毫不保留)

    package com.example.administrator.myapplication40;
    
    import android.annotation.SuppressLint;
    import android.os.StrictMode;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    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 android.os.Handler;
    import android.os.Looper;
    import android.os.Message;
    import android.os.StrictMode;
    
    
    public class MainActivity extends AppCompatActivity {
        TextView resultView = null;
    
        //异步线程执行完毕后,在这里修改UI界面信息
        Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                Bundle data = msg.getData();
                String val = data.getString("value");
                // UI界面的更新等相关操作
                resultView.setText(val);
            }
        };
    
        /**
         * 网络操作相关的子线程
         */
        Runnable networkTask = new Runnable() {
            @Override
            public void run(){
                // 在这里进行 http request.网络请求相关操作
                String temp="";
                temp = getRemoteInfo();
                Message msg = new Message();
                Bundle data = new Bundle();
                data.putString("value",temp);
                msg.setData(data);
                handler.sendMessage(msg);
            }
        };
    
    
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            resultView = (TextView) findViewById(R.id.id_tv);
    
            // 开启一个子线程,进行网络操作
            new Thread(networkTask).start();
    
        }
    
    
    
    
    
        //远程调用Asp.net WebService
        public String getRemoteInfo() {
    
            String rst = "";
    
            // 命名空间
            String nameSpace = "http://tempuri.org/";
            // 调用的方法名称
            String methodName = "HelloWorld2";
            // EndPoint
            String endPoint = "http://210.45.192.243/ws1/Service1.asmx";
            // SOAP Action
            String soapAction = "http://tempuri.org/HelloWorld2";
    
            // 指定WebService的命名空间和调用的方法名
            SoapObject rpc = new SoapObject(nameSpace, methodName);
    
            // 设置需调用WebService接口需要传入的两个参数mobileCode、userId
            rpc.addProperty("x1", "11");
            rpc.addProperty("x2", "27");
    
            // 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    
            envelope.bodyOut = rpc;
            // 设置是否调用的是dotNet开发的WebService
            envelope.dotNet = true;
    
    
            HttpTransportSE transport = new HttpTransportSE(endPoint);
            try {
                // 调用WebService
                transport.call(soapAction, envelope);
            } catch (Exception e) {
                 e.printStackTrace();
            }
    
            // 获取返回的数据
            SoapObject object = (SoapObject) envelope.bodyIn;
            // 获取返回的结果
            rst = object.getProperty(0).toString();
    
            return rst;
    
        }
    
    }
    

      

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.administrator.myapplication40">
    
    
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    
        <uses-permission android:name="android.permission.INTERNET" />
       
    </manifest>
    

      

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.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="com.example.administrator.myapplication40.MainActivity">
    
        <TextView
            android:id="@+id/id_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    </android.support.constraint.ConstraintLayout>
  • 相关阅读:
    用原生JS判断素(质)数,并找出100~1000之间的所有素(质)数
    用原生JS找出所有的水仙花数
    break、continue和return语句的区别
    用原生JS写16进制随机颜色
    JavaScript函数
    JavaScript数组
    JavaScript流程控制
    新的开始,新的历程
    异常处理
    DictionaryBase
  • 原文地址:https://www.cnblogs.com/qqhfeng/p/7390473.html
Copyright © 2020-2023  润新知