TestApi.java
2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.dianping.cat;
import java.io.InputStream;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.unidal.helper.Urls;
import org.unidal.tuple.Pair;
import org.unidal.webres.helper.Files;
import org.unidal.webres.json.JsonArray;
import org.unidal.webres.json.JsonObject;
public class TestApi {
private String build711Url(int id) {
return "http://cat.dianpingoa.com/cat/r/app?op=linechartJson&query1=2015-04-28;" + id + ";;;711;0;;;;;";
}
private String build720Url(int id) {
return "http://cat.dianpingoa.com/cat/r/app?op=linechartJson&query1=2015-04-28;" + id + ";;;720;0;;;;;";
}
private String fetchContent(String url) throws Exception {
InputStream in = Urls.forIO().readTimeout(3000).connectTimeout(3000).openStream(url);
return Files.forIO().readFrom(in, "utf-8");
}
private Pair<Integer, Double> parse(String content) throws ParseException {
JsonObject obj = new JsonObject(content);
JsonArray array = obj.getJSONArray("lineChartDetails");
JsonObject chart = (JsonObject) array.get(0);
return new Pair<Integer, Double>(chart.getInt("accessNumberSum"), chart.getDouble("successRatio"));
}
@Test
public void test() {
List<Item> items = new ArrayList<Item>();
for (int i = 1; i < 711; i++) {
try {
String url1 = build711Url(i);
String url2 = build720Url(i);
Pair<Integer, Double> pair1 = parse(fetchContent(url1));
Pair<Integer, Double> pair2 = parse(fetchContent(url2));
Item item = new Item();
item.setId(i);
item.setCount1(pair1.getKey());
item.setAvg1(pair1.getValue());
item.setCount2(pair2.getKey());
item.setAvg2(pair2.getValue());
items.add(item);
System.out.print(item.toString());
} catch (Exception e) {
}
}
}
private class Item {
private int m_id;
private long m_count1;
private double m_avg1;
private long m_count2;
private double m_avg2;
public void setAvg1(double avg1) {
m_avg1 = avg1;
}
public void setAvg2(double avg2) {
m_avg2 = avg2;
}
public void setCount1(long count1) {
m_count1 = count1;
}
public void setCount2(long count2) {
m_count2 = count2;
}
public void setId(int id) {
m_id = id;
}
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append(m_id).append("\t");
sb.append(m_count1).append("\t").append(m_avg1).append("\t");
sb.append(m_count2).append("\t").append(m_avg2).append("\n");
return sb.toString();
}
}
}