java,get请求和post请求接口获取数据

1.get(无参)请求

/**
     * @desc get请求
     * @param requestUrl 请求地址
     * @return
     */
    public JSONObject getOpenRequest(String requestUrl){
        JSONObject jsonObject = null;
        StringBuffer buffer = new StringBuffer();
        try {
            URL url = new URL(requestUrl);
            // http协议传输
            HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();

            httpUrlConn.setDoOutput(true);
            httpUrlConn.setDoInput(true);
            httpUrlConn.setUseCaches(false);
            // 设置请求方式(GET/POST)
            httpUrlConn.setRequestMethod("GET");
            httpUrlConn.connect();
            // 将返回的输入流转换成字符串
            InputStream inputStream = httpUrlConn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            String str = null;

            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            bufferedReader.close();
            inputStreamReader.close();
            // 释放资源
            inputStream.close();
            inputStream = null;
            httpUrlConn.disconnect();
            jsonObject = JSONObject.fromObject(buffer.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject;
    }

2. POST传递json数据

/**
     * @desc post请求
     * @param requestUrl 请求地址
     * @param jsondata 请求数据
     * @return
     */
    public JSONObject postOpenRequest(String requestUrl, String jsondata){
        JSONObject jsonObject = null;
        StringBuffer buffer = new StringBuffer();
        try {
            URL url = new URL(requestUrl);
            HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();
            httpUrlConn.setUseCaches(false);
            httpUrlConn.setConnectTimeout(3000);
            httpUrlConn.setDoInput(true);
            httpUrlConn.setDoOutput(true);
            httpUrlConn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            httpUrlConn.setRequestMethod("POST");
            httpUrlConn.connect();
            OutputStreamWriter writer = new OutputStreamWriter(httpUrlConn.getOutputStream());
            //发送参数
//            String jsondata = JSON.toJSONString(purchaseDTO);
            writer.write(jsondata);
            writer.flush();
            writer.close();
            InputStream inputStream = null;
            if (httpUrlConn != null) {
                inputStream = httpUrlConn.getInputStream();
            } else {
                throw new IOException("Connection is not established.");
            }
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            String str = null;

            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            bufferedReader.close();
            inputStreamReader.close();
            // 释放资源
            inputStream.close();
            inputStream = null;
            httpUrlConn.disconnect();
            jsonObject = JSONObject.fromObject(buffer.toString());
            if(jsonObject.get("code").equals(200)){
                jsonObject.get("result");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject;
    }

3.调用get请求或post请求接受数据转换成对象

@Override
    public List<WarehouseVO> getWarehouse() {
        SysDict sysDict = sysDictService.getByTypeAndLabel("open_url", "仓库信息");
        String requestUrl = sysDict.getValue();
        // 调用接口
        JSONObject jsonObject = getOpenRequest(requestUrl);
        List<WarehouseVO> warehouses = new ArrayList<>();
        if(jsonObject.get("code").equals(200)){
            Object returnData = jsonObject.get("result");
            List list1 = JSON.parseObject(returnData.toString(), List.class);
            for(int i=0;i<list1.size();i++) {
                WarehouseVO warehouse= JSON.parseObject(JSON.toJSONString(list1.get(i)), WarehouseVO.class);
                warehouses.add(warehouse);
            }
        }
        return warehouses;
    }

    @Override
    public List<OpenInventoryVO> getSkuCode(OrderSkuPurchaseDTO purchaseDTO) {
        SysDict sysDict = sysDictService.getByTypeAndLabel("open_url", "库存集合");
        String requestUrl = sysDict.getValue();
        String jsondata = JSON.toJSONString(purchaseDTO);
        // 调用接口
        JSONObject jsonObject = postOpenRequest(requestUrl, jsondata);
        List<OpenInventoryVO> inventoryVOS = new ArrayList<>();
        if(jsonObject.get("code").equals(200)){
            Object returnData = jsonObject.get("result");
            List list1 = JSON.parseObject(returnData.toString(), List.class);
            for(int i=0;i<list1.size();i++) {
                OpenInventoryVO inventory = JSON.parseObject(JSON.toJSONString(list1.get(i)), OpenInventoryVO.class);
                inventoryVOS.add(inventory);
            }
        }
        return inventoryVOS;

    }

4.测试接口

// 测试销存开放接口,每一分钟
//    @Scheduled(cron="0 */1 * * * ?")
    private void getWarehouse(){
        // 获取订单库存统计数据
        openOrderSkuService.getWarehouse();


        OrderSkuPurchaseDTO purchaseDTO = new OrderSkuPurchaseDTO();
        List<String> skuCodes = new ArrayList<>();
        skuCodes.add("23322255");
        skuCodes.add("23322244");
        purchaseDTO.setSkuCode(skuCodes);
        purchaseDTO.setWarehouseId("1342386839456444418");
        openOrderSkuService.getSkuCode(purchaseDTO);
    }

你可能感兴趣的:(#,JAVA使用,java)