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

蒋鹏鹍
2023-12-01

下面是发送数据方代码:

// 调用
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;
	}

 类似资料: