android 连webservice

package com.ams4android.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import com.ams4android.entity.DeptGroup;
import com.ams4android.entity.SeaechResult;
import com.ams4android.entity.SystemModel;

/**
 * 调用webservice 的类
 * @author star
 *
 */
public class WSLogin {
	    //NameSpace
	    private static final String NAMESPACE="http://www.msunsoft.com/";
	   //调用的方法
	    private static final String METHOD_NAME="Login";
	    //URL
	    private static final String URL="http://10.67.91.12:81/SBK/WebServices/LoginSvc.asmx";
	    private static final String SOAP_ACTION = NAMESPACE + METHOD_NAME;

//	/**
//	 * @param args
//	 */
//	public static void main(String[] args) {
//		WSLogin ws = new WSLogin();
//        System.out.println(ws.getLoginResult("1", "1"));
//	}
   
	public static  SeaechResult<DeptGroup> getLoginResult(String userCode,String password){
		SeaechResult<DeptGroup>  seaechResult  = new SeaechResult<DeptGroup>();
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);   
		request.addProperty("userCode",userCode);   
		request.addProperty("password",password); 
		  
		  SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
	                     envelope.bodyOut=request;
	                     envelope.dotNet = true;
	                     envelope.setOutputSoapObject(request);
	        
	           HttpTransportSE ht = new HttpTransportSE(URL);   
		           ht.debug = true;   
		           try {
					    ht.call(SOAP_ACTION, envelope);
//		        		ht.call(null, envelope);
					    seaechResult.setConMark(true);
					if(envelope.getResponse()!=null){
						WSLogin ws = new WSLogin();
//					    System.out.println(envelope.getResponse());
						ws.parseResult((SoapObject)envelope.getResponse(), seaechResult);
//						return envelope.bodyIn.toString();
		            }
				} catch (IOException e) {
					seaechResult.setConMark(false);
					seaechResult.setErrMessage("服务器连接失败!");
//					// TODO Auto-generated catch block
//					e.printStackTrace();
				} catch (XmlPullParserException e) {
					seaechResult.setConMark(false);
					seaechResult.setErrMessage("服务器返回数据解析失败!");
//					// TODO Auto-generated catch block
//					e.printStackTrace();
				}        
		               
		return seaechResult;
	}
	
	private void  parseResult(SoapObject responseresult,SeaechResult<DeptGroup>  seaechResult){
		String result=null;
//		String errMsg=null;
		String loginResult=null;
		
		for(int i=0;i<responseresult.getPropertyCount();i++){
			SoapObject resultChilds=(SoapObject)responseresult.getProperty(i);
//			System.out.println(resultChilds_A.getPropertyAsString("Result"));
			result = resultChilds.getPropertyAsString("Result");
//			errMsg =  resultChilds.getPropertyAsString("errMsg");
			loginResult =  resultChilds.getPropertyAsString("LoginResult");
//			System.out.println("A--->"+resultChilds_A);
			if(result!=null&&result.trim().equals("True")){//登陆成功
				seaechResult.setLoginMark(true);
				
			SoapObject systemUser = (SoapObject)resultChilds.getProperty("SystemUser");
//			System.out.println("-----------"+systemUser);
			List<DeptGroup> deptGroupList = new ArrayList<DeptGroup>();
			for(int a=0;a<systemUser.getPropertyCount();a++){
				SoapObject deptGroupResult=(SoapObject)systemUser.getProperty(a);
				DeptGroup dg = new  DeptGroup();
				System.out.println(deptGroupResult);
				dg.setDeptId(deptGroupResult.getAttributeAsString("DeptId"));
				dg.setDeptName(deptGroupResult.getAttributeAsString("DeptName"));
				List<SystemModel> systemModelList = new ArrayList<SystemModel>();
				for(int b=0;b<deptGroupResult.getPropertyCount();b++){
					SoapObject systemModelResult=(SoapObject)deptGroupResult.getProperty(b);
					SystemModel sm = new SystemModel();
					sm.setSysid(systemModelResult.getPropertyAsString("sysid"));
					sm.setUserid(systemModelResult.getPropertyAsString("userid"));
					sm.setName(systemModelResult.getPropertyAsString("name"));
					sm.setRoleId(systemModelResult.getPropertyAsString("roleId"));
					sm.setRoleName(systemModelResult.getPropertyAsString("roleName"));
					sm.setDeptId(systemModelResult.getPropertyAsString("deptId"));
					sm.setDeptName(systemModelResult.getPropertyAsString("deptName"));
					sm.setSystemId(systemModelResult.getPropertyAsString("systemId"));
					sm.setSystemName(systemModelResult.getPropertyAsString("systemName"));
					sm.setHospitalId(systemModelResult.getPropertyAsString("hospitalId"));
					sm.setHospitalName(systemModelResult.getPropertyAsString("hospitalName"));
					sm.setHospitalCode(systemModelResult.getPropertyAsString("hospitalCode"));
					sm.setTs(systemModelResult.getPropertyAsString("ts"));
					if("7".equals(sm.getSystemId())){
					systemModelList.add(sm);
					}
				}
				if(systemModelList.size()>0){
				dg.setSystemModelList(systemModelList);
				deptGroupList.add(dg);
				}
//				System.out.println("B--->"+resultChilds_B);
			}
			if(deptGroupList.size()>0){
			seaechResult.setSeaechResultList(deptGroupList);
			}else{//没有可进入的部门
				seaechResult.setLoginMark(false);
				seaechResult.setErrMessage("该用户没有系统权限");
			}
		    }else{//登陆失败
		    	seaechResult.setLoginMark(false);
				switch (Integer.parseInt(loginResult)){
//				case new LoginResult()..:
					seaechResult.setErrMessage("成功登陆");
					break;
				case 2:
					seaechResult.setErrMessage("用户名不存在");
				     break;
				case 3:
					seaechResult.setErrMessage("密码错误");
				     break;
				case 4:
					seaechResult.setErrMessage("用户账号不存在");
				     break;
				case 5:
					seaechResult.setErrMessage("该用户没有系统权限");
				     break;
				}
		    }
		}
		
		System.out.println("Result:"+result);
//		System.out.println("errMsg:"+errMsg);
		System.out.println("LoginResult:"+loginResult);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值