第三方APP调用jeesite接口,传递参数

本文详细介绍了如何从第三方应用程序调用Jeesite接口进行数据交互,包括发送方的代码实现和接收方的数据处理流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第三方APP调用jeesite接口,传递参数


最近使用jeesite 4.x版本开发,遇到该问题,记录一下解决方案

下面是发送数据方代码:

// 调用
public void postJson() {
        LogUtil.info("Start package Json ..." );
        Map map = new HashMap();
        map.put("name", "json");
        map.put("bool", Boolean.TRUE);
        map.put("int", new Integer(1));
        map.put("arr", new String[] { "a", "b" });
        map.put("func", "function(i){ return this.arr[i]; }");

        JSONObject json = JSONObject.fromObject(map);

        LogUtil.info("add param Json to method doPost() ...  json:" + json);
        // 发送 POST 请求 调用接口 参数1 接口名(项目后 不带/) 参数二 json对象
        doPost("getJson", json);
    }
public static void doPost(String method, JSONObject date) {
        HttpClient client = HttpClients.createDefault();
        // 将接口地址和接口方法拼接起来
        String url = "http://192.168.55.55:8980/jeesite/test/getAppScheduled/" + method;
        HttpPost post = new HttpPost(url);
        JSONObject response = null;
        try {
            StringEntity s = new StringEntity(date.toString());
            s.setContentEncoding("UTF-8");
            s.setContentType("application/json");
            post.setEntity(s);
            post.addHeader("content-type", "text/xml");
            // 调用Fa接口
            HttpResponse res = client.execute(post);
            //----------判断是否重定向开始
            int code = res.getStatusLine().getStatusCode();
            String newuri="";
            //以后会使用鉴权处理,首先登陆omc系统
            /*if (code == 302) {
                Header header = res.getFirstHeader("location");// 跳转的目标地址是在 HTTP-HEAD 中的
                newuri = header.getValue() + "?username=F3EDC7D2C193E0B8DCF554C726719ED2&password=235880C505ACCDA5C581A4F4CDB81DA0&param_deviceType=mobileApp&param_lang=zh_CN&__sid="; // 这就是跳转后的地址,再向这个地址发出新申请,以便得到跳转后的信息是啥。
                System.out.println(newuri);
                System.out.println(code);
                post = new HttpPost(newuri);
                post.setHeader("Content-Type", "application/json;charset=UTF-8");
                StringEntity se = new StringEntity(date.toString());
                se.setContentType("application/json");
                post.setEntity(se);

                res = client.execute(post);

                code = res.getStatusLine().getStatusCode();
                System.out.println("login"+code);

            }*/

            if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                LogUtil.info("Success to Calling method doPost()...  " );
            }else{
                LogUtil.info("System OMC may not be in service...  " );
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

下面是接收数据方:

@RequestMapping("/getJson")
	public String getJson(HttpServletRequest request, HttpServletResponse response) {
		StringBuffer json = new StringBuffer();
		String line = null;
		try {
			BufferedReader reader = request.getReader();
			while ((line = reader.readLine()) != null) {
				System.out.println("line:" + line);
				json.append(line);
			}
		} catch (Exception e) {
			System.out.println(e.toString());

		}
		System.out.println(json.toString());
		JSONObject  jasonObject = JSONObject.fromObject(json.toString());
		Map map = (Map)jasonObject;
		List<String> list = (List<String>) map.get("arr");
		return null;
	}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值