访问WebService工具类

// 通用方法
	public String callWebService(String url, String nameSpace,
			String methodName, List<BasicNameValuePair> list) {
		
		String soapAction = nameSpace + methodName;
		//获取对象,设置参数
		SoapObject request = new SoapObject(nameSpace, methodName);
		for (int i = 0; i < list.size(); i++) {
			request.addProperty(list.get(i).getName(), list.get(i).getValue());
		}
		//设置SOAP请求信息(参数部分为SOAP协议版本号,与你要调用的webService中版本号一致)
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.bodyOut = request;
		envelope.dotNet = true;//这个属性是对dotnetwebservice协议的支持,如果dotnet的webservice 不指定rpc方式则用true否则要用false
//        //注册Envelope
//		(new MarshalBase64()).register(envelope);
		//建立连接传输
		AndroidHttpTransport transport = new AndroidHttpTransport(url);
		//或者HttpTransportSE transport = new HttpTransportSE(url);
		transport.debug = true;
		try {
			//调用WebService
			transport.call(soapAction, envelope);
			//使用getResponse方法获得WebService方法的返回结果
			SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
			return result.toString();
		} catch (Exception e) {
			return "ERROR: " + e.getMessage();
		}
	}

你可能感兴趣的:(访问WebService工具类)