String url = "https:///operation";
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
JSONObject outStr = new JSONObject();
outStr.put("method", "getBasicDevInfo");
String[] arrdevSN = {devSn};
outStr.put("devSN", arrdevSN);
String str = outStr.toJSONString();
JSONObject jsonObject = HttpUtil.doPoststr(url, str, headers);
if (jsonObject.getInteger("code") == 0) {
JSONArray data = jsonObject.getJSONArray("data");
for (int k = 0; k < data.size(); k++) {
JSONObject thisData = data.getJSONObject(k);
String devStatus = thisData.getString("devStatus");
if (devStatus.equals("online")){
onLine++;
} else if (devStatus.equals(" offline")) {
offLine++;
}
}
}
请求工具
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
public class HttpUtil {
public static JSONObject doGetstr(String url) {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
JSONObject jsonObject = null;
try {
CloseableHttpResponse response = httpclient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity != null) {
String result = EntityUtils.toString(entity);
jsonObject = JSONObject.parseObject(result);
}
} catch (IOException e) {
e.printStackTrace();
}
return jsonObject;
}
public static JSONObject doGetstr(String url, Map<String, String> headers) {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
if (headers != null) {
Set<Map.Entry<String, String>> entries = headers.entrySet();
for (Map.Entry<String, String> entry : entries) {
httpGet.setHeader(entry.getKey(), entry.getValue());
}
}
JSONObject jsonObject = null;
try {
CloseableHttpResponse response = httpclient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity != null) {
String result = EntityUtils.toString(entity);
jsonObject = JSONObject.parseObject(result);
}
} catch (IOException e) {
e.printStackTrace();
}
return jsonObject;
}
public static JSONObject doPoststr(String url, String outStr, Map<String, String> headers) {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
if (headers != null) {
Set<Map.Entry<String, String>> entries = headers.entrySet();
for (Map.Entry<String, String> entry : entries) {
httpPost.setHeader(entry.getKey(), entry.getValue());
}
}
JSONObject jsonObject = null;
try {
httpPost.setEntity(new StringEntity(outStr, "utf-8"));
CloseableHttpResponse response = httpclient.execute(httpPost);
String result = EntityUtils.toString(response.getEntity(), "utf-8");
jsonObject = JSONObject.parseObject(result);
} catch (IOException e) {
e.printStackTrace();
}
return jsonObject;
}
public static JSONObject doPoststr(String url, String outStr) {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
JSONObject jsonObject = null;
try {
httpPost.setEntity(new StringEntity(outStr, "utf-8"));
CloseableHttpResponse response = httpclient.execute(httpPost);
String result = EntityUtils.toString(response.getEntity(), "utf-8");
jsonObject = JSONObject.parseObject(result);
} catch (IOException e) {
e.printStackTrace();
}
return jsonObject;
}
public static JSONObject doPost(String url, Map<String, String> headers) {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
if (headers != null) {
Set<Map.Entry<String, String>> entries = headers.entrySet();
for (Map.Entry<String, String> entry : entries) {
httpPost.setHeader(entry.getKey(), entry.getValue());
}
}
JSONObject jsonObject = null;
try {
CloseableHttpResponse response = httpclient.execute(httpPost);
String result = EntityUtils.toString(response.getEntity(), "utf-8");
jsonObject = JSONObject.parseObject(result);
} catch (IOException e) {
e.printStackTrace();
}
return jsonObject;
}
}