附上代码
import com.alibaba.fastjson.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
/**
* Created by Administrator on 2019/4/4 0004.
*/
public class te {
public static void main(String[] args){
CloseableHttpClient client = HttpClients.createDefault();
HttpGet get = new HttpGet("http://192.168.0.106:1013/subdevice/vague?str=1");
get.setHeader("Accept","application/json;charset=utf-8");
get.setHeader("Ttoken","f0d23e6e7e2c445390d0f88ec95cbbf3");
try {
HttpResponse httpResponse=client.execute(get);
HttpEntity entity = httpResponse.getEntity();
String data=entity.toString();
System.out.println("获取的数据"+data);
InputStream instreams = entity.getContent();
String str = ConvertStreamToString(instreams);
System.out.println("Response:" + "\n" + str);
JSONObject jsonObject = JSONObject.fromObject(JSON.parseObject(str));
JSONArray family = jsonObject.getJSONArray("monitorList");
for (int i=0;i<family.size();i++){
JSONObject ob = (JSONObject) family.get(i);//得到json对象
System.out.println(ob.getString("monitorName"));
}
System.out.println(family);
} catch (IOException e) {
e.printStackTrace();
}
}
// Convert stream to string
public static String ConvertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
System.out.println("Error=" + e.toString());
} finally {
try {
is.close();
} catch (IOException e) {
System.out.println("Error=" + e.toString());
}
}
return sb.toString();
}
}
检测是否成功,成功取出摄像头名称