Android 客户端与后台数据交互的方式有很多种。今天这里记录一下,与WebService的数据交互。
新建一个简单的WebService 创建方式如下:

创建好的项目是这样的。

我们在里面写几句简单的代码,如下:

这样,我们的WebService就写好了,接下来我们进行发布。右击项目,选择发布。

选择文件系统,这里我直接发布到本地IIS里面了。

这样就发布结束了,接下来,我们直接打开本地 IIS。

右击login.asmx。点击浏览就可以在默认浏览器中打开了。

这样我们的WebService就结束了。接下来是Android 客户端了。
这里我们首先需要一个jar包。这里我用的是 kSOAP2  下载地址: http://www.oschina.net/p/ksoap2+android
下载之后,我们直接把jar包放到libs里,然后右击,在弹出菜单中点击Add As Library.

然后弹窗,点击确定就好了。

下面我们开始写代码。
先写了一个简单的布局页面。

代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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"
tools:context=".MainActivity"> <EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:hint="Account"
android:ems="10"
android:id="@+id/AccounText"
android:layout_marginTop="34dp"
android:layout_alignParentTop="true"
android:layout_alignStart="@+id/PwdText" /> <EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="Password"
android:ems="10"
android:id="@+id/PwdText"
android:layout_marginTop="24dp"
android:layout_below="@+id/AccounText"
android:layout_centerHorizontal="true" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
android:id="@+id/button"
android:layout_below="@+id/PwdText"
android:layout_centerHorizontal="true"
android:layout_marginTop="36dp" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="内容"
android:id="@+id/textView"
android:layout_below="@+id/button"
android:layout_marginTop="24dp"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>
然后是主Activity 代码如下:

下面代码中有几个注意点 NAMESPACE、MYURL、METHODNAME。其中NAMESPACE 对应上面我们WebService命名空间,我这里是默认的命名空间。

MYURL 对应的地址不止当前WebService 的地址,而是引用方法的具体地址 上面已经说过了。METHODNAME  是调用的方法名

package com.example.administrator.login1;

import android.app.Activity;
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.EditText;
import android.widget.TextView; import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE; public class MainActivity extends Activity { private EditText accountet,pwdet;
private Button loginbtn;
private TextView textView;
private myHandler myHandler =new myHandler(); private final String

NAMESPACE

="http://tempuri.org/";                               //WebService 服务器命名空间
private final String

MYURL

="http://172.18.216.7/login/login.asmx";    //方法的详细地址
private final String

METHODNAME

="login1";                                            //具体的方法名称

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView(); //登录按钮
loginbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { new Thread(new Runnable() {
@Override
public void run() { try {
SoapObject request = new SoapObject(

NAMESPACE,METHODNAME

 );

                            request.addProperty("name", accountet.getText().toString());
request.addProperty("pwd", pwdet.getText().toString()); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = request;
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE ht = new HttpTransportSE(

MYURL

);

                            ht.call(

NAMESPACE+METHODNAME

, envelope);
                            //返回对象应为Object 否则会出现转换异常错误

Object

 soapObject = (

Object

) envelope.getResponse();
                            //发送消息更新UI

                            Message msg2 = new Message();
Bundle bundle = new Bundle();
bundle.putString("result", soapObject.toString());
msg2.what = 1;
msg2.setData(bundle);
myHandler.sendMessage(msg2); }catch (Exception e) {
Log.e("lyf",e.toString());
e.printStackTrace();
}
}
}).start();
}
});
} private void initView()
{
accountet= (EditText) this.findViewById(R.id.AccounText);
pwdet= (EditText) this.findViewById(R.id.PwdText);
loginbtn = (Button) this.findViewById(R.id.button);
textView= (TextView) this.findViewById(R.id.textView);
} class myHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
//更新TextView 显示返回结果
textView.setText(msg.getData().getString("result"));
break;
} super.handleMessage(msg);
}
}
}

Android 使用 ksoap2-android 访问WebService(C#)的更多相关文章

  1. 在Android中调用KSOAP2库访问webservice服务出现的服务端传入参数为null的问题解决

    ksoap2-android-3.0.0-jar 第三方库来调用.net 写的Web Service 如果没有参数,那么调用一切顺利,但是如果服务是带参数的,那么服务端接收的参数都是nul.      ...

  2. 在Android中调用KSOAP2库访问webservice服务出现的服务端返回AnyType{}

    最近在做毕业设计的时候,涉及到了安卓端访问web service服务端数据库,并返回一个值,当我把web service测试通过后,想写一个简单的安卓测试程序,来实现服务端数据库访问,通过web se ...

  3. Android实现KSOAP2访问WebService

    Android实现KSOAP2访问WebService 开发工具:Andorid Studio 1.3 运行环境:Android 4.4 KitKat 代码实现 写一个工具类来给主界面使用,作用是使用 ...

  4. Android使用KSOAP2调用WebService及正确导入jar包的问题(转)

    Android使用KSOAP2调用WebService及正确导入jar包的问题(转)     错误信息 最近在学Android使用KSOAP2调用现有的Webservice的方法,期间在网上找了很多代 ...

  5. 【转载】Android通过ksoap2调用.net(c#)的webservice

    转载自:http://www.cnblogs.com/badtree/articles/3242842.html ■下载 ksoap2-android 包 去http://code.google.co ...

  6. Android访问WebService的两种方法

    首先解释一下WebService:WebService是一种基于SOAP协议的远程调用标准.通过WebService可以将不同操作系统平台,不同语言.不同技术整合到一起.详细见:http://baik ...

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

    1.下载Ksoap2,将jar包拷贝到libs目录下.然后右键点击拷贝进来的jar,在弹出菜单中点击Add As Library. 2.在AndroidManifest.xml中添加访问网络的权限 & ...

  8. Android使用ksoap2调用C#中的webservice实现图像上传

    目录: 一. android使用ksoap2调用webservice 二. 异步调用 三. Android使用ksoap2调用C#中的webservice实现图像上传参考方法 四. 图像传输中Base ...

  9. Android局域网访问webservice以及其中的一些问题

    应老师的要求,要做个安卓app,实现备份app上的数据到服务器上的mongodb上,网上搜了下相关的实现方式.利用webservice技术,具体来说就是客户端直接调用服务器端的接口.之前从来没接触这玩 ...

随机推荐

  1. 【转】jQuery异步上传文件

    用了 jQuery Form插件来解决这个问题:http://malsup.com/jquery/form/#code-samples 有没有不用该插件来实现呢? 解决方法: 可以采用HTML5,用j ...

  2. 315Mhz、433Mhz无线遥控信号的解码分析和模拟

    摘要 前段时间学习无线电的同时了解到arduino是作为技能尚未成熟技术宅的我继树莓派又一个不错的选择.于是花了200元购得3块arduino开发板(2*nano&1*uno)和其他传感器等, ...

  3. Merge Intervals

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  4. PAT (Basic Level) Practise:1022. D进制的A+B

    [题目连接] 输入两个非负10进制整数A和B(<=230-1),输出A+B的D (1 < D <= 10)进制数. 输入格式: 输入在一行中依次给出3个整数A.B和D. 输出格式: ...

  5. HDU 1536 sg函数

    S-Nim Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  6. 跨域之-jquery操作

    在JQ进行跨域的操作,用的是jsonp的方式,创建script标签,除了跨域的行为外,本地的操作方式都是xmlHttpRequest.

  7. [转]STL中vector转数组(实际是数组的指针)

    感谢:http://topic.csdn.net/t/20050429/20/3976956.html 感谢:http://yzyanchao.blogbus.com/logs/47796444.ht ...

  8. github 如何合并不同分支

    From: http://stackoverflow.com/questions/1123344/merging-between-forks-in-github 1. 添加remote origina ...

  9. Javascript操作Cookie的脚本 — CookieHelper

    var HttpCookie = function(name, value, expires, path, domain) { if (name) this.Name = name; if (valu ...

  10. Dictionary 序列化与反序列化

    [转:http://blog.csdn.net/woaixiaozhe/article/details/7873582] 1.说明:Dictionary对象本身不支持序列化和反序列化,需要定义一个继承 ...