Android Webservice 开发总结

     最近比较闲,在学习Android开发知识,觉得我们公司以后的业务查询是不是可以支持移动平台,所以自己有个想法,去玩玩在手机平台(android)开发一个小的程序,功能的话就是方便客户和公司员工能够查询公司内部的一些业务数据,比如:运价查询、船舶动态、货物跟踪.....

    目前看来一切问题都不大,但对于我来说最大的问题应该还是程序UI开发,没有这个经验,所以一切重新开始。加油!!!

 

【调用java cxf编写的webservice】

下面是访问Webservice的一段核心代码,Webservice是用java基于CXF开发的。

 结果返回单个字符串Result:

    protected void InvokeWebService() {
        try  
        {   
            String serviceUrl = Constant.URL;   
            String methodName = "getSYfee";   
            SoapObject request = new SoapObject(Constant.NAMESPACE,   
                    methodName);   
            request.addProperty("fromPort", mOrigin.getText().toString());   
            request.addProperty("toPort", mDest.getText().toString());
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(   
                    SoapEnvelope.VER10);    
            envelope.bodyOut = request;
            HttpTransportSE ht = new HttpTransportSE(serviceUrl);   

            ht.call(null, envelope);   
            if (envelope.getResponse() != null)   
            {   
            	String Re = "0";
            	SoapObject soapObject = (SoapObject) envelope.getResponse();  

                if (mBoxType.getText().toString().trim().equals("20GP"))
                {
                	Re = soapObject.getProperty("twgp").toString();
                }
                else if (mBoxType.getText().toString().trim().equals("40GP"))
                {
                	Re = soapObject.getProperty("ftgp").toString();
                }
                else if (mBoxType.getText().toString().trim().equals("40HC"))
                {
                	Re = soapObject.getProperty("fth").toString();
                }
                result = Re;
            }   
            else  
            {   
                result = "-1";   
            }   
        }   
        catch (Exception e)   	
        {   
            result = "-2";   
        }                
    } 

 结果返回多行多列数据集result:

	protected Object CallWebService() {
            Bundle getBundle = getIntent().getExtras();

            String serviceUrl = Constant.URL;   
            String methodName = "getCargoCrack";   
            SoapObject request = new SoapObject(Constant.NAMESPACE,   
                    methodName);   
            request.addProperty("s_consign", getBundle.getString("Pass_Consign"));   
            request.addProperty("s_container", getBundle.getString("Pass_Container"));
            
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(   
                    SoapEnvelope.VER10);    
            envelope.bodyOut = request;
            HttpTransportSE ht = new HttpTransportSE(serviceUrl);   
            //(new MarshalBase64()).register(envelope);//放着没错 
            
			SoapObject result = null;
			try {
				// web service请求
				ht.call(null, envelope);
				// 得到返回结果
				result = (SoapObject) envelope.bodyIn;		
				
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			return result;

		}

【调用.net编写的webservice】

与调用java的webservice最大区别在于,请求时需要传入soapAction

	/**
	 * 请求.net web service
	 */
	public static Object CallWebService() {
		SoapObject result = null;
		try {
	        String serviceUrl = Constant.URL;   
	        String methodName = "wbsLoginUser";  
	        String soapAction = Constant.NAMESPACE + "wbsLoginUser";

	        SoapObject request = new SoapObject(Constant.NAMESPACE,   
	                methodName);   
	        request.addProperty("sUserCode", "admin");   
	        request.addProperty("sPasswd", "a");
	        
	        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(   
	                SoapEnvelope.VER11);    
	        envelope.bodyOut = request;
	        envelope.dotNet = true;
	        HttpTransportSE ht = new HttpTransportSE(serviceUrl);   
	        ht.debug = true;

			// web service请求
			ht.call(soapAction, envelope);
			// 得到返回结果
			result = (SoapObject) envelope.bodyIn;
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return result;
	}


作者:水煮鱼
出处:http://blog.csdn.net/sheshou2

你可能感兴趣的:(exception,android,webservice,String,service)