直接上代码
CookieStore cookieStore = null;
HttpClientContext context = null;
String loginUrl = "http://cn.grustv.com/xxxx";
String getTerminals = "http://cn.grustv.com/xxxx" + System.currentTimeMillis();
String getDeviceStatus = "http://cn.grustv.com/index/terminal/getDeviceStatus?tid=";
String getDeviceStatusEnd = "&type=hlink_status%7Cforward_info";
@Action(value = "/xxxx", results = { @Result(type = "json", params = { "root", "xxxx" }) })
public void getxxxx() {
List<Object> rtmplist = new ArrayList<Object>();
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
CloseableHttpClient client = httpClientBuilder.build();
HttpPost httpPost = new HttpPost(loginUrl);
Map<String, String> parameterMap = new HashMap<String, String>();
parameterMap.put("name", "xxxx");
parameterMap.put("password", "xxxx");
parameterMap.put("remeber", "xxxx");
UrlEncodedFormEntity postEntity = null;
try {
postEntity = new UrlEncodedFormEntity(getParam(parameterMap),"UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
httpPost.setEntity(postEntity);
try {
HttpResponse httpResponse = client.execute(httpPost);
HttpGet httpGet = new HttpGet(getTerminals);
HttpResponse httpResponse1 = client.execute(httpGet);
List<Object> idlist = new ArrayList<Object>();
HttpEntity entity = httpResponse1.getEntity();
if (entity != null) {
String responseString = EntityUtils.toString(entity);
JSONObject jsonInfo = JSONObject.parseObject(responseString);
JSONArray array = JSONArray.parseArray(jsonInfo.getString("list"));
for (int i = 0; i < array.size(); i++) {
idlist.add(array.getJSONObject(i).get("tid"));
}
}
if (!idlist.isEmpty()) {
for (Object object : idlist) {
HttpGet httpTerminal = new HttpGet(getDeviceStatus + object + getDeviceStatusEnd);
HttpResponse httpResponse2 = client.execute(httpTerminal);
HttpEntity entity2 = httpResponse2.getEntity();
if (entity2 != null) {
Map<String,Object> rtmpMap = new HashMap<String,Object>();
String responseString = EntityUtils.toString(entity2);
JSONObject jsonInfo = JSONObject.parseObject(responseString);
if ((boolean) jsonInfo.get("rel")) {
rtmpMap.put("ID" ,(String) object);
JSONObject preview_url = jsonInfo.getJSONObject("preview_url");
rtmpMap.put("Terminal", preview_url.getString("rtmp"));
rtmplist.add(rtmpMap);
}
}
}
}
setCookieStore(httpResponse);
setContext();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
responseData.setParameter("resultList",rtmplist);
}