Android上使用ksoap2支持Web Service服务调用实例
16lz
2022-03-22
Android上使用ksoap2支持Web Service服务调用实例 作者:雨水,日期:2015-1-13 摘要:本文以使用WebXml网站( http://www.webxml.com.cn/)上的查询手机位置功能的Web Service为例,介绍了如何在Android上采用ksoap2库获取第三方Web服务。
背景知识:当前基于RESTful的Web Service越来越多,但是还是有不少基于SOAP的Web Service存在,因此开发基于SOAP的Web Service的需求还广泛存在。
第一步:环境准备 在 http://code.google.com/p/ksoap2-android/去下载依赖库ksoap2-3.2.0.jar,大约150多k,具体的版本可能有所差异,这个影响不大。如果无法访问google网站,可以在其他网站搜索下载。也可以在本文末提供的工程源代码中找到。
第二步:编写layout界面布局文件 创建一个空的Android工程HelloAndroidSoap,然后在res/layout中增加一个main.xml的layout文件,内容如下:
第三步:编写activity处理文件 ksoap的接口调用相对较为简单,这里只把相应的代码贴出来,一看就明白。这里只需要强调一个地方: SoapSerializationEnvelope对象的dotNet的值最好保证为true,否则可能调用失败。即代码中的envelope.dotNet = true部分。 activity的文件名为:HelloAndroidSoapActivity.java
本文的可执行运行的整个工程及源代码可以在这里找到, http://download.csdn.net/detail/gobitan/8358701。
参考资料: 1. http://code.google.com/p/ksoap2-android/ 2. http://www.open-open.com/bbs/view/1320111271749在Android 中使用KSOAP2调用WebService 3. http://www.cnblogs.com/zhangdongzi/archive/2011/04/19/2020688.htmlAndroid与服务器端数据交互(基于SOAP协议整合android+webservice) 4. http://blog.csdn.net/cstester/article/details/4805387天气返回值介绍
背景知识:当前基于RESTful的Web Service越来越多,但是还是有不少基于SOAP的Web Service存在,因此开发基于SOAP的Web Service的需求还广泛存在。
第一步:环境准备 在 http://code.google.com/p/ksoap2-android/去下载依赖库ksoap2-3.2.0.jar,大约150多k,具体的版本可能有所差异,这个影响不大。如果无法访问google网站,可以在其他网站搜索下载。也可以在本文末提供的工程源代码中找到。
第二步:编写layout界面布局文件 创建一个空的Android工程HelloAndroidSoap,然后在res/layout中增加一个main.xml的layout文件,内容如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/query_linerLayout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <EditText android:id="@+id/phone_edittext" android:layout_width="180dp" android:layout_height="45dp" android:hint="请输入11位手机号" android:inputType="phone" android:maxLength="11" /> <Button android:id="@+id/query_location_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="查询归属地" /> </LinearLayout> <TextView android:id="@+id/query_result_textview" android:layout_width="wrap_content" android:layout_height="45dp" /></LinearLayout>在这个布局中,有一个手机号的输入框,一个查询按钮和一个查询结果显示标签。
第三步:编写activity处理文件 ksoap的接口调用相对较为简单,这里只把相应的代码贴出来,一看就明白。这里只需要强调一个地方: SoapSerializationEnvelope对象的dotNet的值最好保证为true,否则可能调用失败。即代码中的envelope.dotNet = true部分。 activity的文件名为:HelloAndroidSoapActivity.java
package cn.dennishucd;import java.io.IOException;import org.ksoap2.SoapEnvelope;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 android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class HelloAndroidSoapActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final EditText phoneEditText = (EditText)findViewById(R.id.phone_edittext); final TextView resultTextView = (TextView)findViewById(R.id.query_result_textview); Button queryBtn = (Button)findViewById(R.id.query_location_button); queryBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {String msg = null;String phone = phoneEditText.getText().toString();if (phone.length() < 11) {msg = "手机号码格式不对,请重新输入!";}msg = queryPhoneLocation(phone);if (msg == null) {msg = "查询服务器出错!";}resultTextView.setText(msg);}}); } private String queryPhoneLocation(String phone) { String nameSpace = "http://WebXml.com.cn/"; String methodName = "getMobileCodeInfo"; SoapObject soapObject = new SoapObject(nameSpace, methodName); soapObject.addProperty("mobileCode", phone); soapObject.addProperty("userID", ""); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.dotNet = true; //very important for compatibility envelope.bodyOut = soapObject; String url = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"; HttpTransportSE htSE = new HttpTransportSE(url); Object response = null; try { String soapAction = "http://WebXml.com.cn/getMobileCodeInfo";htSE.call(soapAction, envelope);response = envelope.getResponse();} catch (HttpResponseException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} catch (XmlPullParserException e) {e.printStackTrace();} if (response != null) { Log.d("HelloAndroidSoap", "The invoke result is: " + response.toString()); return response.toString(); } else { return null; } }}第四步:运行并测试 运行会出现界面,在编辑框中输入你的手机号,界面将显示手机的归属地。如下: 18030639225:四川 成都 四川电信CDMA卡,这个号码是我随即输入的。
本文的可执行运行的整个工程及源代码可以在这里找到, http://download.csdn.net/detail/gobitan/8358701。
参考资料: 1. http://code.google.com/p/ksoap2-android/ 2. http://www.open-open.com/bbs/view/1320111271749在Android 中使用KSOAP2调用WebService 3. http://www.cnblogs.com/zhangdongzi/archive/2011/04/19/2020688.htmlAndroid与服务器端数据交互(基于SOAP协议整合android+webservice) 4. http://blog.csdn.net/cstester/article/details/4805387天气返回值介绍
更多相关文章
- android SD卡学习3-读写文件
- Android(安卓)从硬件到应用:一步一步向上爬 4 -- 使用 JNI 方法调
- [置顶] Android启动过程——init,Zygote,SystemServer
- Android下修改hosts文件
- Android的View和ViewGroup分析
- android activity开发文档翻译 - 1 - 基础篇
- Android编译系统(一、环境与命令)
- App与Js交互(二)Android
- NPM 和webpack 的基础使用