日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android 使用Ksoap2工具类实现WebService网络编程

發布時間:2025/7/14 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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"/><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:supportsRtl="true"android:theme="@style/AppTheme"><activityandroid: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"><EditTextandroid:id="@+id/orgCode_txt"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="請輸入要查詢的組織編碼" /><Buttonandroid:id="@+id/get_btn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="獲取組織信息" /><TextViewandroid: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() {@Overridepublic void onClick(View v) {WebServiceUtils.getOrgInfo(orgCodeTxt.getText().toString(), new WebServiceUtils.CallBack() {@Overridepublic 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) {// 用于子線程與主線程通信的Handlerfinal Handler mHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);// 將返回值回調到callBack的參數中 callBack.result((String) msg.obj);}};new Thread(new Runnable() {@Overridepublic void run() {// 命名空間String nameSpace = "http://tempuri.org/";// 調用的方法名稱String methodName = "GetOrgInfo";// EndPointString endPoint = "http://192.168.1.12:8037/IWMSService.asmx?wsdl";// SOAP Actionfinal 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需要設置成trueenvelope.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();} }

?

轉載于:https://www.cnblogs.com/shijunzhang/p/4985176.html

總結

以上是生活随笔為你收集整理的android 使用Ksoap2工具类实现WebService网络编程的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。