Android访问WebService-CXF步骤

本文介绍了如何在Android应用中集成WebService进行用户认证,包括添加依赖库、配置访问工具类、创建服务层和设计登录界面。文章详细阐述了通过WebService调用服务器端方法进行用户验证的过程,并提供了完整的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.将ksoap2-android-assembly-3.0.0-jar-with-dependencies.jar包添加到Android项目的libs目录下
2.Web Service的工具类WebServiceHelper
import java.util.HashMap;
import java.util.Map.Entry;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.annotation.SuppressLint;
import android.os.Build;
import android.os.StrictMode;
/**
 * 访问Web Service的工具类
 * @author jCuckoo
 * 
 */
@SuppressLint("NewApi")
public class WebServiceHelper {
	static {
		if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.ICE_CREAM_SANDWICH){
			// 4.0以后需要加入下列两行代码,才可以访问Web Service
			StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
					.detectDiskReads().detectDiskWrites().detectNetwork()
					.penaltyLog().build());

			StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
					.detectLeakedSqlLiteObjects().detectLeakedClosableObjects()
					.penaltyLog().penaltyDeath().build());
		}
		//4.0以前版本不需要以上设置
	}
	/**
	 * @param url          web service路径
	 * @param nameSpace    web service名称空间
	 * @param methodName   web service方法名称
	 * @param params       web service方法参数
	 */
	public static SoapObject getSoapObject(String serviceName,
			String methodName, String soapAction, HashMap<String, Object> params) {
		String URL = "http://192.168.1.89:8080/MyWebService/webservice/"+ serviceName + "?wsdl";
		String NAMESPACE = "http://webservice.dh.com/";// 名称空间,服务器端生成的namespace属性值
		String METHOD_NAME = methodName;
		String SOAP_ACTION = soapAction;

		SoapObject soap = null;
		try {
			SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);
			if (params != null && params.size() > 0) {
				for (Entry<String, Object> item : params.entrySet()) {
					rpc.addProperty(item.getKey(), item.getValue().toString());
				}
			}

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			envelope.bodyOut = rpc;
			envelope.dotNet = false;// true--net; false--java;
			envelope.setOutputSoapObject(rpc);

			HttpTransportSE ht = new HttpTransportSE(URL);
			ht.debug = true;
			ht.call(SOAP_ACTION, envelope);
			try {
				soap = (SoapObject) envelope.getResponse();
			} catch (Exception e) {
				soap = (SoapObject) envelope.bodyIn;
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return soap;
	}
}
3.创建service层LoginService
import java.util.HashMap;
import org.ksoap2.serialization.SoapObject;
import android.content.Context;
import android.widget.Toast;

import com.dh.util.WebServiceHelper;

public class LoginService {
	public void login(Context context,String userName,String userPwd){
		HashMap<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("userName", userName);
		paramsMap.put("userPwd", userPwd);
		                                                      // 服务的名称    方法名    soapAction--null   参数数据
		SoapObject soapOjbect = WebServiceHelper.getSoapObject("UserService", "checkUser", null, paramsMap);
		if(soapOjbect!=null){
			Toast.makeText(context, soapOjbect.getPropertyAsString(0), Toast.LENGTH_LONG).show();
		}
	}
}
4.设计布局界面activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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" 
    android:orientation="vertical">
	<LinearLayout android:id="@+id/Layout_top"
	    android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
	    <TextView android:text="用户名:"
	        android:layout_width="fill_parent"
	        android:layout_height="wrap_content"/>
	    <EditText android:id="@+id/userName"
	        android:layout_width="fill_parent"
	        android:layout_height="wrap_content"/>
	</LinearLayout>
	<LinearLayout android:id="@+id/Layout_middle"
	    android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
	    <TextView android:text="密码:"
	        android:layout_width="fill_parent"
	        android:layout_height="wrap_content"/>
	    <EditText android:id="@+id/userPwd"
	        android:layout_width="fill_parent"
	        android:layout_height="wrap_content"/>
	</LinearLayout>
	<Button android:id="@+id/login_Button"
	    android:text="登陆"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content" />
</LinearLayout>
5.编写程序主界面MainActivity
import android.app.Activity;
import android.os.Bundle;
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;

import com.dh.service.LoginService;

public class MainActivity extends Activity {
	private EditText userNameEditText;
	private EditText userPwdEditText;
	private Button loginButton;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		userNameEditText=(EditText) findViewById(R.id.userName);
		userPwdEditText=(EditText)findViewById(R.id.userPwd);
		loginButton=(Button) findViewById(R.id.login_Button);
		loginButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				String userName=userNameEditText.getText().toString();
				String userPwd=userPwdEditText.getText().toString();
				if("".equals(userName)&&"".equals(userPwd)){
					Toast.makeText(MainActivity.this, "用户名或密码不能为空", Toast.LENGTH_LONG).show();
				}
				LoginService loginService=new LoginService();
				loginService.login(getApplicationContext(), userName, userPwd);
				
			}
		});
	}
}
6.在AndroidManifest.xml中设定网络访问权限
<!-- 设置使用web service等的权限 -->
    <uses-permission android:name="android.permission.INTERNET" />








评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值