android 調用webservice
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、新建一個登陸的布局界面
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/main_bg" android:orientation="vertical" > <ScrollView android:id="@+id/Main_LinearLayout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_above="@+id/bottommenu" android:layout_alignParentTop="true" > <LinearLayout android:id="@+id/Main_LinearLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ImageView android:id="@+id/TitleBG" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/title_bg" > </ImageView> <TextView android:id="@+id/system_name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:paddingTop="20dip" android:text="@string/login_name" android:textColor="@drawable/black" android:textSize="30sp" > </TextView> <TableLayout android:id="@+id/TableLayout01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingLeft="20dip" android:paddingRight="20dip" android:paddingTop="20dip" android:shrinkColumns="1" android:stretchColumns="1" > <TableRow android:paddingTop="15dip" > <TextView android:id="@+id/TextView01" android:padding="10dip" android:text="@string/user_code_text" android:textColor="@drawable/black" android:textSize="18sp" > </TextView> <EditText android:id="@+id/user_code" android:hint="@string/username_hint" android:padding="10dip" android:textSize="16sp" /> </TableRow> <TableRow android:paddingTop="15dip" > <TextView android:id="@+id/TextView02" android:padding="10dip" android:text="@string/password_text" android:textColor="@drawable/black" android:textSize="18sp" > </TextView> <EditText android:id="@+id/user_pwd" android:hint="@string/password_hint" android:padding="10dip" android:password="true" android:textSize="16sp" /> </TableRow> <TableRow android:gravity="center" android:paddingTop="10dip" > <CheckBox android:id="@+id/remember_pwd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="@string/rememberpwd_text" android:textColor="@drawable/black" /> </TableRow> </TableLayout> </LinearLayout> </ScrollView> <LinearLayout android:id="@id/bottommenu" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="horizontal" android:padding="10dip" > <Button android:id="@+id/login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/login_text" > </Button> <Button android:id="@+id/exit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/exit_text" > </Button> </LinearLayout> </RelativeLayout>
第二步:建立登陸的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; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.login); findViews(); loadConfig(); setOnClickListener(); } @Override public void onClick(View v) { // TODO Auto-generated method stub } public void setOnClickListener() { /* 為 Button 注冊點擊事件監聽對象,采用了匿名內部類的方式。 */ login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { login(); } } ); /* 為 Button 注冊點擊事件監聽對象,采用了匿名內部類的方式。 */ 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); // outlineLogin = (CheckBox) // contextActivity.findViewById(R.id.outline_login); } @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); //log("SSSSSSSSSSSS"); } 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) { // TODO Auto-generated method stub 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) { // TODO Auto-generated method stub 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) { // TODO Auto-generated method stub 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) { // TODO Auto-generated method stub 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( String nameSpace, String methodName, Map<String, Object> params) { SoapObject request = new SoapObject(nameSpace, methodName);// 第一步,實例化SoapObject對象,并指定命名空間和方法名 if (params != null && !params.isEmpty()) { for (Iterator<Map.Entry<String, Object>> it = params.entrySet() .iterator(); it.hasNext();) { Map.Entry<String, Object> e = it.next(); if (e.getValue() instanceof byte[]) { byte[] d = (byte[]) e.getValue(); String data = new String(Base64.encode(d)); // request.addProperty(e.getKey(), new // SoapPrimitive(SoapEnvelope.ENC, "base64Binary", data)); request.addProperty(e.getKey(), data);// 第二步,設置調用參數方法,包括參數名稱和參數值 } else { request.addProperty(e.getKey().toString(), e.getValue()); } } } // 第三步,設置SOAP請求信息(參數部分為SOAP協議版本號,與你要調用到的WEBSERVICE中的版本號一致) SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER10); envelope.setOutputSoapObject(request); // envelope.bodyOut = request; envelope.dotNet = true; envelope.encodingStyle = SoapSerializationEnvelope.ENC; return envelope; } public static SoapPrimitive getSoapPrimitiveByCallingWSWithFile( String nameSpace, String methodName, Map<String, Object> params, String wsdl) throws IOException, XmlPullParserException { final String SOAP_ACTION = nameSpace + methodName; SoapPrimitive soapPrimitiveResult = null; SoapObject request = constructRequestObjectWithFile(nameSpace, methodName, params); SoapSerializationEnvelope envelope = getEncEnvelope(request); soapPrimitiveResult = (SoapPrimitive) callWebservice(SOAP_ACTION, wsdl, envelope); return soapPrimitiveResult; } public static Object callWebservice(String SOAP_ACTION, String wsdl, SoapSerializationEnvelope envelope) throws IOException, XmlPullParserException { // registerObjects(envelope); Object resultObject = null; HttpTransportSE ht = new HttpTransportSE(wsdl); // 第四步,構建傳輸對象,并指明wsdl文檔url try { ht.call(SOAP_ACTION, envelope);// 第五步,調用webservice(其中參數一SOAP_ACTION為命名空間+方法名,參數二為envelope) } catch (IOException e) { Log.e("IOException:", e.getMessage()); // androidHT.reset(); throw e; } catch (XmlPullParserException e1) { Log.e("XmlPullParserException", e1.getMessage()); throw e1; } try { resultObject = envelope.getResponse();// 第6步:使用getResponse方法獲得WebService方法的返回結果 } catch (SoapFault e) { Log.e("SoapFault", e.getMessage()); } return resultObject; } public static SoapObject constructRequestObject(String nameSpace, String methodName, Map<String, Object> params) { SoapObject request = new SoapObject(nameSpace, methodName); if (params != null && !params.isEmpty()) { for (Iterator<Map.Entry<String, Object>> it = params.entrySet() .iterator(); it.hasNext();) { Map.Entry<String, Object> e = it.next(); request.addProperty(e.getKey().toString(), e.getValue()); } } return request; } public static SoapObject constructRequestObjectWithFile(String nameSpace, String methodName, Map<String, Object> params) { SoapObject request = new SoapObject(nameSpace, methodName); if (params != null && !params.isEmpty()) { for (Iterator<Map.Entry<String, Object>> it = params.entrySet() .iterator(); it.hasNext();) { Map.Entry<String, Object> e = it.next(); if (e.getValue() instanceof byte[]) { byte[] d = (byte[]) e.getValue(); String data = new String(Base64.encode(d)); request.addProperty(e.getKey(), new SoapPrimitive( SoapEnvelope.ENC, "base64Binary", data)); } else { request.addProperty(e.getKey().toString(), e.getValue()); } } } return request; } public static SoapSerializationEnvelope getEncEnvelope(SoapObject request) { SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); envelope.setOutputSoapObject(request); // envelope.bodyOut = request; envelope.dotNet = true; envelope.encodingStyle = SoapSerializationEnvelope.ENC; return envelope; } public static SoapSerializationEnvelope getEnvelope(SoapObject request) { // SoapSerializationEnvelope envelope=new // SoapSerializationEnvelope(SoapEnvelope.VER10);//SOAP 1.0 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER10);// SOAP 1.1 // SoapSerializationEnvelope envelope=new // SoapSerializationEnvelope(SoapEnvelope.VER12);//SOAP 1.2 envelope.dotNet = true; // envelope.bodyOut = request; envelope.setOutputSoapObject(request); return envelope; } public static Map<String, String> getDatabyWebservicesMap( Activity activity, HashMap<String, Object> params, String servies) { SoapObject queryResult = null; ArrayList<List<String>> rows = null; try { queryResult = WSUtil.getSoapObjectByCallingWS( WebServiceConfig.NAMESPACE, servies, params, WebServiceConfig.wsdl); } catch (Exception e) { /* * new * AlertDialog.Builder(activity.getApplicationContext()).setTitle * (R.string.message_title) .setMessage(R.string.connection_error) * .setPositiveButton(R.string.OK_text, null).show(); * activity.finish(); */ // return; } return WSUtil.handleResultMap(queryResult); } public static Map<String, String> handleResultMap(SoapObject queryResult) { WSObjectUtil wsObjectUtil = new WSObjectUtil(); SoapObject dataset = WSUtil.getDataRow(queryResult, 0); if (dataset == null) { // noResult(); return null; } return (Map<String, String>) WSUtil.getPropertiesMap(dataset); } public static SoapObject getDataRow(SoapObject o, Integer index) { SoapObject row = null; try { SoapObject diffgram = (SoapObject) o.getProperty(1); SoapObject rows = (SoapObject) diffgram.getProperty(0); row = (SoapObject) rows.getProperty(index); } catch (Exception e) { Log.e("Exception", e.getMessage()); } return row; } public static Map<String, String> getPropertiesMap(SoapObject o) { Map<String, String> propertiesMap = new HashMap<String, String>(); for (int i = 0; i < o.getPropertyCount(); i++) { PropertyInfo propertyInfo = new PropertyInfo(); o.getPropertyInfo(i, propertyInfo); String propertyName = propertyInfo.getName(); propertiesMap.put(propertyName, o.getProperty(i).toString()); } return propertiesMap; } public static ArrayList<List<String>> handleResult(SoapObject queryResult) { WSObjectUtil wsObjectUtil = new WSObjectUtil(); SoapObject dataset = wsObjectUtil.getDataTableObject(queryResult); if (dataset == null) { // noResult(); return null; } ArrayList<List<String>> rowparameter = new ArrayList<List<String>>(); rowparameter = (ArrayList<List<String>>) WSUtil .getPropertiesTableList(dataset); return rowparameter; } public static List<List<String>> getPropertiesTableList(SoapObject o) { List<List<String>> propertiesTableList = new ArrayList<List<String>>(); try { for (int i = 0; i < o.getPropertyCount(); i++) { propertiesTableList.add(getPropertiesList((SoapObject) o .getProperty(i))); } } catch (Exception e) { Log.e("Exception", e.getMessage()); } return propertiesTableList; } public static List<String> getPropertiesList(SoapObject o) { List<String> propertiesList = new ArrayList<String>(); try { for (int i = 0; i < o.getPropertyCount(); i++) { propertiesList.add(o.getProperty(i).toString()); } } catch (Exception e) { Log.e("Exception", e.getMessage()); } return propertiesList; } }
以上的紅色字體部分就是具體調用webservice的具體步驟,最后一步,也就是第六步將會提供結果給調用webservice的方法,然后login()方法將通過
result = WSUtil.getSoapObjectByCallingWS(
WebServiceConfig.NAMESPACE, "login", params,
WebServiceConfig.wsdl);
得到第六步返回的結果。
當返回的結果與登陸界面輸入的帳號和密碼匹配后,登陸就成功了。
當然與服務器交互,這里還需要寫接口,關鍵是其中的sql語句,我是在visual studio 2008上用c#寫的一個login接口,如下:
[WebMethod(Description = "Android 接口: 登陸 userName 用戶名 userPwd 用戶密碼")] public DataSet login(string userName, string userPwd) { DataSet ds = null; string strSql = string.Format("select userid, username, password, deptid, realname, tel, email, usermemo, starttime, endtime, mapid, dwid, zw, deptname, purviewmid, dwmc from V_USERS t where t.username = '{0}' and t.password='{1}'", userName, userPwd); try { RetStrSql(strSql); ds = RetDataSet(); return ds; } catch (Exception ex) { //throw new Exception(ex.Message); return null; } }
好的,基本就是這樣,貼上代碼共享地址:http://download.csdn.net/detail/jindegegesun/4070721
轉自:http://blog.csdn.net/jindegegesun/article/details/7267813