package test;
//這些事導入的一些包,需要引用一些jar才行
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class HttpTest {
// 獲取帶有參數的json串
public static String getHttpResult(String url, String parm) {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url + parm);
String json = null;
try {
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
json = EntityUtils.toString(entity, "UTF-8").trim();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
httpget.abort();
}
return json;
}
/* 獲取沒有參數的json串
public static String getHttpResult(String url) {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
String json = null;
try {
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
json = EntityUtils.toString(entity, "UTF-8").trim();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
httpget.abort();
}
return json;
}
*/
//此方法用來獲取某個交換機的流表信息
public static void ParseJson(String url,String switchid,String jsonString) {
JSONObject jsonobj = JSONObject.fromObject(jsonString); // 將json字符串轉換成jsonObject對象
JSONArray flowsOfSwitch=jsonobj.getJSONArray(switchid);//把交換機parm號的流表信息轉換成Json數組
System.out.println("交換機"+switchid+"的流表信息如下:");
for (int i = 0; i < flowsOfSwitch.size(); i++) {//依次取出1號交換機的每一個流表
int j=i+1;
System.out.println("第"+j+"條流表:");
System.out.println("actions:");
System.out.println("actions:"+flowsOfSwitch.getJSONObject(0).getJSONArray("actions").get(0));
System.out.println("idle_timeout:"+flowsOfSwitch.getJSONObject(0).get("idle_timeout"));
System.out.println("cookie:"+flowsOfSwitch.getJSONObject(0).get("cookie"));
System.out.println("packet_count:"+flowsOfSwitch.getJSONObject(0).get("packet_count"));
System.out.println("hard_timeout:"+flowsOfSwitch.getJSONObject(0).get("hard_timeout"));
System.out.println("byte_count:"+flowsOfSwitch.getJSONObject(0).get("byte_count"));
System.out.println("duration_sec:"+flowsOfSwitch.getJSONObject(0).get("duration_sec"));
System.out.println("duration_nsec:"+flowsOfSwitch.getJSONObject(0).get("duration_nsec"));
System.out.println("priority:"+flowsOfSwitch.getJSONObject(0).get("priority"));
System.out.println("length:"+flowsOfSwitch.getJSONObject(0).get("length"));
System.out.println("flags:"+flowsOfSwitch.getJSONObject(0).get("flags"));
System.out.println("table_id:"+flowsOfSwitch.getJSONObject(0).get("table_id"));
System.out.println("match:");
System.out.println("dl_dst:"+flowsOfSwitch.getJSONObject(0).getJSONObject("match").get("dl_dst"));
System.out.println("dl_src:"+flowsOfSwitch.getJSONObject(0).getJSONObject("match").get("dl_src"));
System.out.println("in_port:"+flowsOfSwitch.getJSONObject(0).getJSONObject("match").get("in_port"));
}
}
/*
* 依次取出JSONArray中的值 private static void getJsonArrayItem(JSONArray array) { for
* (int i=0; i<array.size(); i++) { System.out.println(array.get(i)); } }
*/
public static void main(String args[]) {
/* 獲取網絡拓撲中的所有交換機,第一次返回的是json串,第二次返回的是解析後的數組
String url = "http://192.168.177.129:8080/stats/switches";
String result = getHttpResult(url);
JSONArray jsonarray = JSONArray.fromObject(result);
System.out.println("未經加工的json數組:" + jsonarray);// 返回或取得數據,也就是json串
for (int i = 0; i < jsonarray.size(); i++) {
System.out.println("解析出來的json數組中的元素:" + jsonarray.get(i));// 返回解析好的json內的數據,其實獲得的是一個數組,get(index)就是獲取數組中的第幾位
}
*/
/* 獲取1號交換機的所有流表信息
String parm = "1";
String url1 = "http://192.168.177.130:8080/stats/flow/";
String jsonString = getHttpResult(url1, parm);
System.out.println("未經解析的交換機1的流表信息:" + jsonString);
*/
String switchid1 = "1";
String url1 = "http://192.168.177.130:8080/stats/flow/";
String jsonString1 = getHttpResult(url1, switchid1);
ParseJson(url1,switchid1,jsonString1);
}
}
解析在網站獲取的JSON數據串,獲取交換機的流表信息並解析出來
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.