java向webService(java)接口请求并接收数据

import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Node;

import com.css.apps.base.dict.service.DictMan;
import com.css.util.StringHelper;

public class sendHttp{
	private static Log log = LogFactory.getLog(sendHttpToIssueOa.class);
	public static Map<String, List<String>> sendHttpTest(String loginName,String flag,String number) {
	 	String namespace = "http://service.cssbaseHomePage.xx.xxx.css.com";
	 	String url="http://xxx.xxx.xxx.x:8080/xxxxx/xxxxx/xxxxxxx?wsdl";
		String _xmlInfo="<?xml version=\"1.0\" encoding=\"utf-8\"?><Root><Document><LoginName>"+loginName+"</LoginName><Flag>"+flag+"</Flag><Number>"+number+"</Number></Document></Root>";
		Service service=new Service();
		Call call;
		String res ="";
		Map<String, List<String>> issueOa = new HashMap<String, List<String>>();
		try{
			call = (Call)service.createCall();
			call.setOperationName(new QName(namespace,"getFileTitle"));
			call.setTargetEndpointAddress(new URL(url));
			call.addParameter(new QName(namespace,"_xmlInfo"), org.apache.axis.Constants.XSD_STRING, ParameterMode.IN);
			call.getParameterTypeByName("_xmlInfo");
			res=(String)call.invoke(new Object[] {_xmlInfo});
			issueOa = getIssueOa(res,flag);
		}catch(Exception e){
			e.printStackTrace();
			log.error(e.getMessage(), e);
		}
		return issueOa;
	}
	 
	//获取到接口返回来的值
	@SuppressWarnings({ "unchecked", "null" })
	public static Map<String, List<String>> getIssueOa(String xml,String flag){
		Document doc = null;
		Map<String, List<String>> map = null;
		List<String> errorFlag = null;
		if (StringHelper.isEmptyByTrim(xml)) {
			map.put("xml为空", errorFlag);
			return map;
		}
		try {
			doc = DocumentHelper.parseText(xml);
			if (doc != null) {
				map = new HashMap<String, List<String>>();
				List<Node> sendPendingName = doc.selectNodes("/result/ColumnName/messageText");
				if (sendPendingName == null || sendPendingName.size() < 1) {
					map.put("sendColumnName获取失败", errorFlag);
					return map;
				}
				List<String> columnNameList = new ArrayList<>();
				for(int i=0;i<sendPendingName.size();i++){
					String str = sendPendingName.get(i).getStringValue().trim();
					columnNameList.add(str);
					map.put("columnName", columnNameList);
				}
				// 此处需要添加逻辑,返回成功的条码号
				/*if ("1".equals(code)) {
					returnString = "1";
				} else {
					returnString = "调用接口失败";
				}*/
			} else {
				map.put("解析xml结果为空", errorFlag);
				return map;
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		return map;
	}
}

 

转载于:https://my.oschina.net/u/3748375/blog/3081663

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值