java调接口传输报文并获取报文数据

package nc.vo.ewm.supperaddinv;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.httpclient.HttpStatus;
import org.json.XML;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import nc.bs.dao.BaseDAO;
import nc.bs.pub.pa.PreAlertObject;
import nc.bs.pub.taskcenter.BgWorkingContext;
import nc.bs.pub.taskcenter.IBackgroundWorkPlugin;
import nc.impl.bd.supplier.baseinfo.SupplierBaseInfoExtendImpl;
import nc.jdbc.framework.processor.BeanProcessor;
import nc.ui.bd.pub.extend.PrivateServiceContext;
import nc.vo.bd.supplier.SupplierVO;
import nc.vo.pub.BusinessException;

public class SupplierInset implements IBackgroundWorkPlugin{
	String http = "http://192.168.3.7:8040/service/XChangeServlet?account=01&groupcode=0";
	@Override
	public PreAlertObject executeTask(BgWorkingContext var1) throws BusinessException {
			try {
				SupplierSave(http);
			} catch (Exception e) {
				throw new BusinessException(e);
			}
		return null;
	}
	public void SupplierSave(String urlStr) throws Exception {
		String errorMsg = null;
		// 创建url资源
		URL resturl = new URL(urlStr);
		// 建立http连接
		HttpURLConnection conns = (HttpURLConnection) resturl.openConnection();
		// 设置允许输出
		conns.setDoOutput(true);
		conns.setDoInput(true);
		// 设置传递方式
		conns.setRequestMethod("POST");
		// 设置维持长连接
		conns.setRequestProperty("Connection", "Keep-Alive");
		// 设置文件类型:
		conns.setRequestProperty("Content-Type", "text/xml");
		//conns.setRequestProperty("Accept", "application/json");
			
		//开始接收报文数据
		String xmlobj = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + 
				"<ufinterface account=\"01\" groupcode=\"0\" billtype=\"suppquery\" isexchange=\"Y\" sender=\"default\">\r\n" + 
				"<bill id=\"pfxx_suppquery\">\r\n" + 
				"<billhead>\r\n" + 
				"<calldate>2021-06-22 14:21:04</calldate>\r\n" + 
				"</billhead>\r\n" + 
				"</bill>\r\n" + 
				"</ufinterface>";
		OutputStream out = (OutputStream) conns.getOutputStream();
		if (out != null) {
			out.write(xmlobj.getBytes("utf-8"));
			out.flush();
			out.close();
		}
			
		// 请求返回的状态
		if (conns.getResponseCode() == HttpStatus.SC_OK) {
			nc.bs.logging.Logger.error("连接成功:" + conns.getResponseCode());
			// 请求返回的数据
			InputStream in = (InputStream) conns.getInputStream();
			
			String ret = null;
			SupplierBaseInfoExtendImpl supplier = new SupplierBaseInfoExtendImpl();
			byte[] data1 = new byte[in.available()];
			in.read(data1);
			ret = new String(data1, "utf8");

			nc.bs.logging.Logger.error("retdata:" + ret);
			
			org.json.JSONObject response = XML.toJSONObject(ret);
			org.json.JSONObject obj = response.getJSONObject("ufinterface");
			org.json.JSONObject obj2 = obj.getJSONObject("sendresult");
			String obj3 = obj2.getString("content");  
			JSONArray jsonArray = JSON.parseArray(obj3);
			
			for (int i = 0; i < jsonArray.size(); i++) {
				JSONObject jsonvo = (JSONObject) jsonArray.get(i);
				SupplierVO vo = new SupplierVO();
				
				Map<String, Object> extendObjMap = new HashMap();
				PrivateServiceContext extendContext = new PrivateServiceContext();
				Integer en = jsonvo.getInteger("enablestate");
				String code = jsonvo.getString("code");
				String name = jsonvo.getString("name");
				BaseDAO dao = new BaseDAO();
				SupplierVO update = (SupplierVO) dao.executeQuery("select * from BD_SUPPLIER where code = '"+code+ "_a"+"'", new BeanProcessor(SupplierVO.class));
				if(update != null) {
					update.setCode(code +"_an");
					update.setName(name +"_an");
					update.setShortname(name+"_an");
					update.setPk_group("0001A710000000000CE9");
					update.setSupbankacc(null);
					supplier.updateSupplierWithExtendVO(update, extendObjMap, extendContext, false);
				}else {
					vo.setEnablestate(en);
					vo.setPk_oldsupplier(null);
					vo.setPk_country("0001Z010000000079UJJ");
					vo.setPk_format("FMT0Z000000000000000");
					vo.setPk_currtype("1002Z0100000000001K1");
					vo.setPk_org("0001A710000000000CE9");
					vo.setPk_supplierclass("1001A710000000001KLA");
					vo.setPk_timezone("0001Z010000000079U2P");
					vo.setPk_group("0001A710000000000CE9");
					vo.setCode(code+"_a");
					vo.setDef22(null);
					vo.setName(name+"_a");
					supplier.insertSupplierWithExtendVO(vo , extendObjMap , extendContext , false);
				}
				
			}
		} else {
			errorMsg = "发送数据失败,返回值:" + conns.getResponseCode() + "\n" 
				+ conns.getResponseMessage() == null ? "" : conns.getResponseMessage().toString();
			nc.bs.logging.Logger.error(errorMsg);
			throw new BusinessException(errorMsg);
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值