Android访问WebService-CXF步骤

文章来源:http://blog.csdn.net/guoquanyou/article/details/9290609

1.将ksoap2-android-assembly-3.0.0-jar-with-dependencies.jar包添加到Android项目的libs目录下
2.Web Service的工具类WebServiceHelper

[java]  view plain copy
  1. import java.util.HashMap;  
  2. import java.util.Map.Entry;  
  3. import org.ksoap2.SoapEnvelope;  
  4. import org.ksoap2.serialization.SoapObject;  
  5. import org.ksoap2.serialization.SoapSerializationEnvelope;  
  6. import org.ksoap2.transport.HttpTransportSE;  
  7. import android.annotation.SuppressLint;  
  8. import android.os.Build;  
  9. import android.os.StrictMode;  
  10. /** 
  11.  * 访问Web Service的工具类 
  12.  * @author jCuckoo 
  13.  *  
  14.  */  
  15. @SuppressLint("NewApi")  
  16. public class WebServiceHelper {  
  17.     static {  
  18.         if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.ICE_CREAM_SANDWICH){  
  19.             // 4.0以后需要加入下列两行代码,才可以访问Web Service  
  20.             StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()  
  21.                     .detectDiskReads().detectDiskWrites().detectNetwork()  
  22.                     .penaltyLog().build());  
  23.   
  24.             StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()  
  25.                     .detectLeakedSqlLiteObjects().detectLeakedClosableObjects()  
  26.                     .penaltyLog().penaltyDeath().build());  
  27.         }  
  28.         //4.0以前版本不需要以上设置  
  29.     }  
  30.     /** 
  31.      * @param url          web service路径 
  32.      * @param nameSpace    web service名称空间 
  33.      * @param methodName   web service方法名称 
  34.      * @param params       web service方法参数 
  35.      */  
  36.     public static SoapObject getSoapObject(String serviceName,  
  37.             String methodName, String soapAction, HashMap<String, Object> params) {  
  38.         String URL = "http://192.168.1.89:8080/MyWebService/webservice/"+ serviceName + "?wsdl";  
  39.         String NAMESPACE = "http://webservice.dh.com/";// 名称空间,服务器端生成的namespace属性值  
  40.         String METHOD_NAME = methodName;  
  41.         String SOAP_ACTION = soapAction;  
  42.   
  43.         SoapObject soap = null;  
  44.         try {  
  45.             SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);  
  46.             if (params != null && params.size() > 0) {  
  47.                 for (Entry<String, Object> item : params.entrySet()) {  
  48.                     rpc.addProperty(item.getKey(), item.getValue().toString());  
  49.                 }  
  50.             }  
  51.   
  52.             SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);  
  53.             envelope.bodyOut = rpc;  
  54.             envelope.dotNet = false;// true--net; false--java;  
  55.             envelope.setOutputSoapObject(rpc);  
  56.   
  57.             HttpTransportSE ht = new HttpTransportSE(URL);  
  58.             ht.debug = true;  
  59.             ht.call(SOAP_ACTION, envelope);  
  60.             try {  
  61.                 soap = (SoapObject) envelope.getResponse();  
  62.             } catch (Exception e) {  
  63.                 soap = (SoapObject) envelope.bodyIn;  
  64.             }  
  65.         } catch (Exception ex) {  
  66.             ex.printStackTrace();  
  67.         }  
  68.         return soap;  
  69.     }  
  70. }  
3.创建service层LoginService
[java]  view plain copy
  1. import java.util.HashMap;  
  2. import org.ksoap2.serialization.SoapObject;  
  3. import android.content.Context;  
  4. import android.widget.Toast;  
  5.   
  6. import com.dh.util.WebServiceHelper;  
  7.   
  8. public class LoginService {  
  9.     public void login(Context context,String userName,String userPwd){  
  10.         HashMap<String, Object> paramsMap = new HashMap<String, Object>();  
  11.         paramsMap.put("userName", userName);  
  12.         paramsMap.put("userPwd", userPwd);  
  13.                                                               // 服务的名称    方法名    soapAction--null   参数数据  
  14.         SoapObject soapOjbect = WebServiceHelper.getSoapObject("UserService""checkUser"null, paramsMap);  
  15.         if(soapOjbect!=null){  
  16.             Toast.makeText(context, soapOjbect.getPropertyAsString(0), Toast.LENGTH_LONG).show();  
  17.         }  
  18.     }  
  19. }  
4.设计布局界面activity_main.xml
[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:paddingBottom="@dimen/activity_vertical_margin"  
  7.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  8.     android:paddingRight="@dimen/activity_horizontal_margin"  
  9.     android:paddingTop="@dimen/activity_vertical_margin"  
  10.     tools:context=".MainActivity"   
  11.     android:orientation="vertical">  
  12.     <LinearLayout android:id="@+id/Layout_top"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:orientation="horizontal">  
  16.         <TextView android:text="用户名:"  
  17.             android:layout_width="fill_parent"  
  18.             android:layout_height="wrap_content"/>  
  19.         <EditText android:id="@+id/userName"  
  20.             android:layout_width="fill_parent"  
  21.             android:layout_height="wrap_content"/>  
  22.     </LinearLayout>  
  23.     <LinearLayout android:id="@+id/Layout_middle"  
  24.         android:layout_width="wrap_content"  
  25.         android:layout_height="wrap_content"  
  26.         android:orientation="horizontal">  
  27.         <TextView android:text="密码:"  
  28.             android:layout_width="fill_parent"  
  29.             android:layout_height="wrap_content"/>  
  30.         <EditText android:id="@+id/userPwd"  
  31.             android:layout_width="fill_parent"  
  32.             android:layout_height="wrap_content"/>  
  33.     </LinearLayout>  
  34.     <Button android:id="@+id/login_Button"  
  35.         android:text="登陆"  
  36.         android:layout_width="fill_parent"  
  37.         android:layout_height="wrap_content" />  
  38. </LinearLayout>  
5.编写程序主界面MainActivity
[java]  view plain copy
  1. import android.app.Activity;  
  2. import android.os.Bundle;  
  3. import android.view.Menu;  
  4. import android.view.View;  
  5. import android.view.View.OnClickListener;  
  6. import android.widget.Button;  
  7. import android.widget.EditText;  
  8. import android.widget.Toast;  
  9.   
  10. import com.dh.service.LoginService;  
  11.   
  12. public class MainActivity extends Activity {  
  13.     private EditText userNameEditText;  
  14.     private EditText userPwdEditText;  
  15.     private Button loginButton;  
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.activity_main);  
  20.         userNameEditText=(EditText) findViewById(R.id.userName);  
  21.         userPwdEditText=(EditText)findViewById(R.id.userPwd);  
  22.         loginButton=(Button) findViewById(R.id.login_Button);  
  23.         loginButton.setOnClickListener(new OnClickListener() {  
  24.               
  25.             @Override  
  26.             public void onClick(View arg0) {  
  27.                 String userName=userNameEditText.getText().toString();  
  28.                 String userPwd=userPwdEditText.getText().toString();  
  29.                 if("".equals(userName)&&"".equals(userPwd)){  
  30.                     Toast.makeText(MainActivity.this"用户名或密码不能为空", Toast.LENGTH_LONG).show();  
  31.                 }  
  32.                 LoginService loginService=new LoginService();  
  33.                 loginService.login(getApplicationContext(), userName, userPwd);  
  34.                   
  35.             }  
  36.         });  
  37.     }  
  38. }  
6.在AndroidManifest.xml中设定网络访问权限
[html]  view plain copy
  1. <!-- 设置使用web service等的权限 -->  
  2.     <uses-permission android:name="android.permission.INTERNET" />  
Android访问WebService-CXF步骤_第1张图片


你可能感兴趣的:(Android访问WebService-CXF步骤)