private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
public static String postJsonHTTP(String boxIP,String port ,String jsonStr,String requestHead) throws IOException {
String url = "http://"+boxIP+":"+port+"/"+requestHead;
OkHttpClient okHttpClient = new OkHttpClient.Builder().readTimeout(30, TimeUnit.SECONDS).build();
RequestBody requestBody = RequestBody.create(JSON, jsonStr);
Request.Builder builder = new Request.Builder();
final Request request = builder.url(url).post(requestBody).build();
final Call call = okHttpClient.newCall(request);
Response response;
response = call.execute();
return response.body().string();
}
如何调用
public List<Map> requestCameraList() throws Exception{
String boxIP = "192.168.88.133";
String port = "8080";
Map requestMap = new HashMap();
requestMap.put("Name","SubDevicesManageRequest");
String ret = postJsonHTTP(boxIP, port, gson.toJson(requestMap),LOADBAIDUCAMERA);
HashMap retMap = gson.fromJson(ret, HashMap.class);
System.err.println(retMap);
return cameraListMap;
}