use of net.sf.json.JSONObject in project blueocean-plugin by jenkinsci.
the class ExportTest method test_json.
@Test
public void test_json() throws IOException {
String xJson = Export.toJson(new X());
JSONObject jsonObj = JSONObject.fromObject(xJson);
Assert.assertEquals(X.class.getName(), jsonObj.getString("_class"));
Assert.assertEquals("xVal", jsonObj.getString("val"));
}
use of net.sf.json.JSONObject in project blueocean-plugin by jenkinsci.
the class SSEConnection method subscribe.
public void subscribe(String channel) throws IOException {
// constructor is not public
// EventFilter f = new EventFilter();
JSONObject o = new JSONObject();
o.put("jenkins_channel", channel);
EventFilter f = (EventFilter) o.toBean(EventFilter.class);
configure(Collections.singletonList(f), Collections.<EventFilter>emptyList());
}
use of net.sf.json.JSONObject in project blueocean-plugin by jenkinsci.
the class SSEConnection method toJSONArray.
private JSONArray toJSONArray(Collection<EventFilter> filters) {
JSONArray a = new JSONArray();
for (EventFilter f : filters) {
JSONObject o = new JSONObject();
o.putAll(f);
a.add(o);
}
return a;
}
use of net.sf.json.JSONObject in project pmph by BCSquad.
the class HttpRequestUtil method httpRequest.
/**
* 发起https请求并获取结果
*
* @param requestUrl 请求地址
* @param requestMethod 请求方式(GET、POST)
* @param outputStr 提交的数据
* @return JSONObject(通过JSONObject.get(key)的方式获取json对象的属性值)
*/
public static JSONObject httpRequest(String requestUrl, String requestMethod, String outputStr) {
System.err.println(requestMethod + "\toutputStr=" + outputStr);
JSONObject jsonObject = null;
StringBuffer buffer = new StringBuffer();
try {
// 创建SSLContext对象,并使用我们指定的信任管理器初始化
TrustManager[] tm = { new MyX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
// 从上述SSLContext对象中得到SSLSocketFactory对象
SSLSocketFactory ssf = sslContext.getSocketFactory();
URL url = new URL(requestUrl);
HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
httpUrlConn.setSSLSocketFactory(ssf);
httpUrlConn.setDoOutput(true);
httpUrlConn.setDoInput(true);
httpUrlConn.setUseCaches(false);
// 设置请求方式(GET/POST)
httpUrlConn.setRequestMethod(requestMethod);
if ("GET".equalsIgnoreCase(requestMethod))
httpUrlConn.connect();
// 当有数据需要提交时
if (null != outputStr) {
OutputStream outputStream = httpUrlConn.getOutputStream();
// 注意编码格式,防止中文乱码
outputStream.write(outputStr.getBytes("UTF-8"));
outputStream.close();
}
// 将返回的输入流转换成字符串
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());
// System.out.println("jsonObject="+jsonObject);
} catch (ConnectException ce) {
logger.error("网络链接:{}", ce.getMessage());
} catch (UnknownHostException uhe) {
logger.error("微信API访问异常:{}", uhe.getMessage());
// httpRequest(requestUrl, requestMethod, outputStr);
} catch (Exception e) {
logger.error("出现异常:{}", e.getMessage());
}
return jsonObject;
}
use of net.sf.json.JSONObject in project pmph by BCSquad.
the class TestSendMes method Send_msg.
/**
* <pre>
* 功能描述:主动发送文字给企业用户
* 使用示范:
*
* @param touser 成员ID列表
* @param toparty 部门ID列表
* @param totag 标签ID列表
* @param msgtype 消息类型,此时固定为:text (支持消息型应用跟主页型应用)
* @param agentid 企业应用的id,整型。可在应用的设置页面查看
* @param content 消息内容,最长不超过2048个字节,注意:主页型应用推送的文本消息在微信端最多只显示20个字(包含中英文)
* @return int 表示是否是保密消息,0表示否,1表示是,默认0
* </pre>
*/
public static int Send_msg(String touser, String toparty, String totag, String msgtype, int agentid, String content) {
int errCode = 0;
// 拼接请求地址
String requestUrl = SEND_MSG_URL.replace("ACCESS_TOKEN", WechatAccessToken.getAccessToken(Constants.CORPID, Constants.SECRET, 1).getToken());
// 需要提交的数据
// String postJson =
// "{\"agentid\":\"%s\",\"touser\":\"%s\",\"toparty\":\"[1,%s]\",\"totag\":\"%s\",\"msgtype\":\"text\",\"%s\":{\"content\":\"%s\"},\"safe\":\"0\"}";
// String postJson =
// "{\"agentid\":\"%s\",\"touser\":\"%s\",\"toparty\":\"%s\",\"totag\":\"%s\",\"msgtype\":\"text\",\"%s\":{\"content\":\"%s\"},\"safe\":\"0\"}";
String postJson = "{\"agentid\":%s,\"touser\": \"%s\",\"toparty\": \"%s\",\"totag\": \"%s\"," + "\"msgtype\":\"%s\",\"text\": {\"content\": \"%s\"},\"safe\":0}";
String outputStr = String.format(postJson, agentid, touser, toparty, totag, msgtype, content);
// System.out.println(outputStr);
// 创建成员
JSONObject jsonObject = HttpRequestUtil.httpRequest(requestUrl, "POST", outputStr);
if (null != jsonObject) {
// System.out.println(jsonObject.toString() + "=====");
// int errcode = jsonObject.getInt("errcode");
errCode = jsonObject.getInt("errcode");
}
return errCode;
}
Aggregations