http://blog.csdn.net/jindegegesun/article/details/7267813
android端与服务器交互 一般情况下,我们可以使用SOCKET,HTTP(GET,POST)等,我们也可以使用另外一种方式,webservice,
它是一种基于SAOP协议的远程调用标准,通过webservice可以将不同操作系统平台,不同语言,不同技术整合到一起。说白了就是一种中间件技术.
我们在android客户端中,有时需要一些库,比如XFire,Axis2,CXF等等来支持访问WebService,由于android sdk等并没有提供这些库,所以并不适合我们资源有限的android手机客户端,这里有KSOAP这个第三方的类库,可以帮助我们获取服务器端webService调用,KSOAP已经提供了基于android版本的jar包.
先下载KSOAP包:ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar包
然后新建一个android项目:并把下载的KSOAP包放在android项目的lib目录下:右键->build path->configure build path--选择Libraries,如图:
以下分为七个步骤来调用WebService方法:
第一:实例化SoapObject 对象,指定webService的命名空间(从相关WSDL文档中可以查看命名空间),以及调用方法名称。
public final static String NAMESPACE = "http://tempuri.org/";
public static String wsdl ="http://jinhesoft.com/Service1.asmx";
private final static String defaultAddress ="jinhesoft.com";
private final static String defaultPage = "service1.asmx";
public final static String CONFIG_PATH = "/data/data/org.DigitalCM/DigitalCMWebserviceConfig.dat";
第二步:假设方法有参数的话,设置调用方法参数
request.addProperty("参数名称","参数值");
第三步:设置SOAP请求信息(参数部分为SOAP协议版本号,与你要调用的webService中版本号一致):
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER10);
envelope.setOutputSoapObject(request);
// envelope.bodyOut = request;
第四步:构建传输对象,并指明WSDL文档URL:
HttpTransportSE ht = new HttpTransportSE(wsdl);
第五步:调用WebService(其中参数为1:命名空间+方法名称,2:Envelope对象):
ht.call(SOAP_ACTION, envelope);// 调用webservice(其中参数一SOAP_ACTION为命名空间+方法名,参数二为envelope)
第六步:解析返回数据:
resultObject = envelope.getResponse();// 第7步:使用getResponse方法获得WebService方法的返回结果
原理和步骤过程了解后,我们下面结合具体项目实例给大家分享下,我们还是以之前的一篇文章: 登陆界面软件自动更新功能的实现http://blog.csdn.net/jindegegesun/article/details/7232779
为例进行,简化更改一个登陆过程。
1、新建一个登陆的布局界面
第二步:建立登陆的Activity
- package cn.jindegegesun.activity;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Properties;
-
-
- import org.DigitalCM.entities.UserInfo;
- import org.ksoap2.serialization.SoapObject;
-
- import cn.jindegegesun.util.PropertyUtil;
- import cn.jindegegesun.webservice.WSObjectMapUtil;
- import cn.jindegegesun.webservice.WSObjectUtil;
- import cn.jindegegesun.webservice.WSUtil;
- import cn.jindegegesun.webservice.WebServiceConfig;
- import android.app.AlertDialog;
- import android.app.Dialog;
- import android.app.ProgressDialog;
- import android.app.SearchManager.OnCancelListener;
- import android.app.SearchManager.OnDismissListener;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.CheckBox;
- import android.widget.EditText;
-
- public class DigitalCity extends ContextActivity implements OnClickListener {
-
- public final static int PROGRESS_DIALOG = 1;
- public final static int DIALOG_LOGIN_FAIELD = 2;
- public final static int DIALOG_CONNECT_ERROR = 3;
- public final static int DIALOG_USER_PWD_EMPTY = 4;
- public final static int DIALOG_EXIT_PROMPT = 5;
- public final static int DIALOG_VERSION_UPDATE = 6;
- private Button login = null;
- private Button exit = null;
- private EditText userCode = null;
- private EditText password = null;
- private CheckBox rememberpwd = null;
-
-
-
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.login);
-
- findViews();
- loadConfig();
- setOnClickListener();
-
- }
-
- @Override
- public void onClick(View v) {
-
-
- }
-
- public void setOnClickListener() {
-
-
- login.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- login();
-
- }
- }
- );
-
-
- exit.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- exitApp();
- }
- });
- }
- public void findViews() {
- userCode = (EditText) findViewById(R.id.user_code);
- password = (EditText) findViewById(R.id.user_pwd);
- rememberpwd = (CheckBox)
- findViewById(R.id.remember_pwd);
- login = (Button) findViewById(R.id.login);
- exit = (Button) findViewById(R.id.exit);
-
-
- }
-
- @SuppressWarnings("static-access")
- public void login() {
-
- new Thread(){
- public void run(){
- if (!validate(userCode.getText().toString(), password.getText()
- .toString()));
-
-
- Map<String, Object> params = new HashMap<String, Object>();
- params.put("userName", userCode.getText().toString().trim()
- .toLowerCase());
- params.put("userPwd", password.getText().toString());
-
- SoapObject result = null;
- try {
- result = WSUtil.getSoapObjectByCallingWS(
- WebServiceConfig.NAMESPACE, "login", params,
- WebServiceConfig.wsdl);
-
- } catch (Exception ex) {
- showDialog(DIALOG_CONNECT_ERROR);
-
- Log.e("Exception", ex.getMessage());
-
- }
-
- if (result == null) {
- loginFailed();
-
- }
-
- WSObjectUtil wsObjectUtil = new WSObjectUtil();
- SoapObject dataSet = null;
- try {
- dataSet = wsObjectUtil.getDataSetObject(result);
- } catch (Exception e) {
- loginFailed();
-
- }
-
- if (dataSet == null) {
- loginFailed();
-
- }
- List<Map<String, Object>> rowMapList = WSObjectMapUtil
- .getRowMapList(dataSet);
-
- UserInfo.setUserInfo(rowMapList.get(0));
- Intent intent =new Intent(DigitalCity.this,Main.class);
- startActivity(intent);
-
- }
- }.start();
-
-
-
- }
-
- private Boolean validate(String user_code, String password) {
- if (user_code == null || user_code.trim().equals("")
- || password == null || password.trim().equals("")) {
- showDialog(DIALOG_USER_PWD_EMPTY);
- return false;
- }
-
- return true;
- }
-
- public void loadConfig() {
-
- Properties properties = PropertyUtil
- .loadConfig(WebServiceConfig.CONFIG_PATH);
- String username = properties.getProperty("username");
- String pwd = properties.getProperty("password");
-
- properties = PropertyUtil.loadConfig(WebServiceConfig.CONFIG_PATH);
- username = properties.getProperty("username");
- pwd = properties.getProperty("password");
-
- if (pwd != null && !pwd.equals("")) {
- rememberpwd.setChecked(true);
- password.setText(pwd);
- } else {
- rememberpwd.setChecked(false);
- }
-
- userCode.setText(username);
- }
-
- private void loginFailed() {
- showDialog(DIALOG_LOGIN_FAIELD);
- }
-
- private void exitApp() {
- showDialog(DIALOG_EXIT_PROMPT);
- }
-
- @Override
- protected Dialog onCreateDialog(int id) {
-
- switch(id){
- case LoginActions.DIALOG_LOGIN_FAIELD:
- return new AlertDialog.Builder(this)
- .setTitle(R.string.error_title)
- .setMessage(R.string.login_failed)
- .setPositiveButton(R.string.OK_text,
- new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog,
- int arg1) {
-
- dialog.cancel();
- }
- }).show();
- case LoginActions.DIALOG_CONNECT_ERROR:
- return new AlertDialog.Builder(this)
- .setTitle(R.string.message_title)
- .setMessage(R.string.connection_error)
- .setPositiveButton(R.string.OK_text,
- new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog,
- int which) {
-
- dialog.cancel();
- }
-
- }).show();
- case LoginActions.DIALOG_USER_PWD_EMPTY:
- return new AlertDialog.Builder(this)
- .setTitle(R.string.message_title)
- .setMessage(R.string.usercode_or_password_empty_warning)
- .setPositiveButton(R.string.OK_text,
- new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog,
- int which) {
-
- dialog.cancel();
- }
-
- }).show();
- case LoginActions.DIALOG_EXIT_PROMPT:
- return new AlertDialog.Builder(this)
- .setTitle(R.string.message_title)
- .setMessage(R.string.exit_prompt)
- .setPositiveButton(R.string.OK_text,
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog,
- int which) {
- finish();
- }
- })
- .setNegativeButton(R.string.cancel_text,
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog,
- int which) {
- dialog.cancel();
- }
- }).show();
- }
- return super.onCreateDialog(id);
- }
-
-
- }
第三步:新建一个登陆进入后的布局文件main.xml以及Main.Activity,这个类和界面都很简单
- <?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" >
- <TextView android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:text="hello,jindegege!"/>"
-
-
- </LinearLayout>
- package cn.jindegegesun.activity;
-
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.Window;
-
-
-
-
-
- public class Main extends Activity {
-
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.main);
-
-
-
-
- }
- }
第四步:添加程序所用的权限:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="cn.jindegegesun.activity"
- android:versionCode="1"
- android:versionName="1.0" >
-
- <uses-sdk android:minSdkVersion="7" />
-
- <application
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name" >
- <activity
- android:name=".DigitalCity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
-
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity
- android:label="@string/login_name"
- android:name="cn.jindegegesun.activity.Main" >
- </activity>
- </application>
- <uses-permission android:name="android.permission.INTERNET" />
- </manifest>
注意:在第一个DigitalCity .activity的login()方法调用了webservice返回的数据,先贴出代码;
- package cn.jindegegesun.webservice;
-
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import java.util.Vector;
-
- import org.kobjects.base64.Base64;
- import org.ksoap2.SoapEnvelope;
- import org.ksoap2.SoapFault;
- import org.ksoap2.serialization.PropertyInfo;
- 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.app.Activity;
- import android.util.Log;
-
- public class WSUtil {
- public static Boolean getBooleanByCallingWS(String nameSpace,
- String methodName, Map<String, Object> params, String wsdl)
- throws IOException, XmlPullParserException {
- return (Boolean) getObjectByCallingWS(nameSpace, methodName, params,
- wsdl);
- }
-
- @SuppressWarnings("unchecked")
- public static Vector<SoapObject> getSoapObjectVectorByCallingWS(
- String nameSpace, String methodName, Map<String, Object> params,
- String wsdl) throws IOException, XmlPullParserException {
- return (Vector<SoapObject>) getObjectByCallingWS(nameSpace, methodName,
- params, wsdl);
- }
-
- public static SoapObject getSoapObjectByCallingWS(String nameSpace,
- String methodName, Map<String, Object> params, String wsdl)
- throws IOException, XmlPullParserException {
- return (SoapObject) getObjectByCallingWS(nameSpace, methodName, params,
- wsdl);
- }
-
- public static SoapPrimitive getSoapPrimitiveByCallingWS(String nameSpace,
- String methodName, Map<String, Object> params, String wsdl)
- throws IOException, XmlPullParserException {
- return (SoapPrimitive) getObjectByCallingWS(nameSpace, methodName,
- params, wsdl);
- }
-
- public static Object getObjectByCallingWS(String nameSpace,
- String methodName, Map<String, Object> params, String wsdl)
- throws IOException, XmlPullParserException {
-
- final String SOAP_ACTION = nameSpace + methodName;
- Object soapPrimitiveResult = null;
-
- SoapSerializationEnvelope envelope = constructRequestObject2(nameSpace,
- methodName, params);
- soapPrimitiveResult = callWebservice(SOAP_ACTION, wsdl, envelope);
-
- return soapPrimitiveResult;
- }
-
- private static SoapSerializationEnvelope constructRequestObject2(
-