Android中 调用WebService详解 ksoap2-android
来源:互联网 发布:scratch少儿趣味编程 编辑:程序博客网 时间:2024/06/05 23:39
首先下载ksoap2-android jar包,下载地址为:https://code.google.com/p/ksoap2-android/
在项目下面建立一个libs文件夹,将刚刚下载的jar包放进去,再将其加入到工程的Build Path中
1. 布局文件/res/layout/activity_main.xml
<?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:gravity="center_horizontal" android:orientation="vertical" android:layout_margin="0px" android:padding="0px"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:text="用户登录" android:textColor="#3f8f3f" android:textSize="30sp"/> <TableLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="80dp" android:stretchColumns="0,3" > <TableRow android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:gravity="center" > <TextView /> <TextView android:layout_width="80dp" android:layout_height="wrap_content" android:gravity="left|center_vertical" android:text="用户名:" android:textColor="#3f8f3f" android:textSize="20sp" /> <EditText android:id="@+id/username" android:layout_width="160dp" android:layout_height="38dp" android:gravity="left|center_vertical" android:textColor="#3f8fff" android:textSize="18sp" /> <TextView /> </TableRow> <TableRow android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="15dp" android:gravity="center" > <TextView /> <TextView android:layout_width="80dp" android:layout_height="wrap_content" android:gravity="left|center_vertical" android:text="密 码:" android:textColor="#3f8f3f" android:textSize="20sp" /> <EditText android:id="@+id/password" android:layout_width="160dp" android:layout_height="38dp" android:inputType="textPassword" android:gravity="left|center_vertical" android:textColor="#3f8fff" android:textSize="18sp" /> <TextView /> </TableRow> </TableLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:orientation="horizontal" android:layout_marginTop="10dp"> <Button android:id="@+id/btn1" android:layout_width="80dp" android:layout_height="35dp" android:layout_marginRight="10dp" android:gravity="center" android:text="登录" android:textColor="#3f8f3f" /> <Button android:id="@+id/btn2" android:layout_width="80dp" android:layout_height="35dp" android:layout_marginLeft="10dp" android:gravity="center" android:text="注册" android:textColor="#3f8f3f" /> </LinearLayout></LinearLayout>
效果图:
package com.bibi.webservice;import java.io.IOException;import org.ksoap2.SoapEnvelope;import org.ksoap2.serialization.SoapObject;import org.ksoap2.serialization.SoapPrimitive;import org.ksoap2.serialization.SoapSerializationEnvelope;import org.ksoap2.transport.HttpTransportSE;import org.xmlpull.v1.XmlPullParserException;import android.os.Bundle;import android.app.Activity;import android.app.AlertDialog;import android.app.Dialog;import android.content.DialogInterface;import android.text.Editable;import android.util.Log;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity {private EditText username;private EditText password;private Button btn1;private Button btn2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);username=(EditText) findViewById(R.id.username);password=(EditText) findViewById(R.id.password);btn1=(Button) findViewById(R.id.btn1);btn2=(Button) findViewById(R.id.btn2);btn1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {String uname=username.getText().toString().trim();String upass=password.getText().toString().trim();if(validate(uname, upass)){if(login(uname, upass)){Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();show_Dialog("登陆成功");}else{Toast.makeText(MainActivity.this, "登陆失败", Toast.LENGTH_SHORT).show();show_Dialog("登录失败");}}}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}public boolean login(String username, String password) {boolean result = false;//1.创建HttpTransportSE对象,该对象用于调用Web Service操作HttpTransportSE htse = new HttpTransportSE("http://192.168.137.1:8080/WebServiceTest/UserDAOPort?wsdl");htse.debug = true;//2.使用SOAP1.1协议创建Envelope对象SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);//3.创建SoapObject对象,该对象需要传入Web Service的命名空间、方法名SoapObject request = new SoapObject("http://dao.bibi.com/", "login");//4.设置传给WebService的参数request.addProperty("arg0", username);request.addProperty("arg1", password);//5.将SoapObject设置为Envelope的传出SOAP消息体envelope.bodyOut=request;//6.调用HttpTransportSE对象的call方法,并将Envelope做为参数,调用远程Web Servicetry {//调用WebServicehtse.call(null, envelope);if (envelope.getResponse() != null) {//7.获取服务器响应返回的SOAP消息体SoapObject response = (SoapObject) envelope.bodyIn;Log.i("====response====", response.toString());//====response==== loginResponse{return=true; }SoapPrimitive sp = (SoapPrimitive) response.getProperty(0);Log.i("====getProperty====", response.getProperty(0).toString());//====getProperty==== trueLog.i("====sp====", sp.toString());//====sp==== trueif(("true").equals(sp.toString())){result = true;}}} catch (IOException ioe) {Log.i("==dddddd=",ioe.getMessage());ioe.printStackTrace();} catch (XmlPullParserException xppe) {Log.i("==dddddd123=",xppe.getMessage());xppe.printStackTrace();}return result;}public void show_Dialog(String msg){AlertDialog.Builder dialog =new AlertDialog.Builder(this);dialog.setTitle(msg);dialog.setCancelable(true);dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubToast.makeText(MainActivity.this, "点击了确定", Toast.LENGTH_SHORT).show();}});dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubToast.makeText(MainActivity.this, "点击了取消", Toast.LENGTH_SHORT).show();}});//dialog.show();AlertDialog alert=dialog.create();alert.show();}public boolean validate(String username,String password){if(username.equals("")){show_Dialog("用户名不能为空!");return false;}if(password.equals("")){show_Dialog("密码不能为空!");return false;}return true;}}
3.访问网络的权限
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.bibi.webservice" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="10" /> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.bibi.webservice.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
在调用某些WebService时,可能在第87行代码中,htse.call(null, envelope);
调用WebService时并不需要传入soapAction,将其置为null即可。
- Android中 调用WebService详解 ksoap2-android
- android ksoap2调用webservice
- 在Android 中使用KSOAP2调用WebService
- 在Android 中使用KSOAP2调用WebService
- 在Android 中使用KSOAP2调用WebService
- 在Android 中使用KSOAP2调用WebService
- 在Android 中使用KSOAP2调用WebService
- 在Android 中使用KSOAP2调用WebService
- 在Android 中使用KSOAP2调用WebService
- 在Android 中使用KSOAP2调用WebService
- 在Android 中使用KSOAP2调用WebService
- 在Android 中使用KSOAP2调用WebService
- 在Android 中使用KSOAP2调用WebService
- 在Android 中使用KSOAP2调用WebService
- 在Android 中使用KSOAP2调用WebService
- 在Android 中使用KSOAP2调用WebService
- 在Android 中使用KSOAP2调用WebService
- Android 中利用 ksoap2 调用 WebService
- n-1位数
- Android 接口和抽象类的区别
- 经典c程序100例(11--20)
- 几个简单数学分布
- Windows server 2003 安全设置
- Android中 调用WebService详解 ksoap2-android
- 浅谈程序猿的职业规划,看你如何决定自己的未来吧。
- 读朱兆祺攻破C语言之五---关键字、运算符、语句
- 开始进行 64 位 Windows 系统编程之前需要了解的所有信息
- 优化邮件营销标题的实战技巧
- Core Audio APIs 技术笔记一(扬声器音量设置)
- java中的各种数据类型在内存中存储的方式
- 模拟spring容器实例化
- Oracle创建表空间、创建用户以及授权、查看权限