-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathGsonTest.java
More file actions
38 lines (30 loc) · 1.19 KB
/
GsonTest.java
File metadata and controls
38 lines (30 loc) · 1.19 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
import com.google.gson.Gson;
import model.LatestNews;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* Created by Administrator on 2016/5/17.
*/
public class GsonTest {
public static final String urlString = "http://news-at.zhihu.com/api/4/news/latest";
public static void main(String[] args) {
LatestNews latest = new LatestNews();
String jsonString = new String(HttpUtil.get(urlString));
long startTime = System.currentTimeMillis();
latest = (new Gson()).fromJson(jsonString, LatestNews.class);
long endTime = System.currentTimeMillis();
double time = (double) (endTime - startTime) / 1000.0;
System.out.println("took " + time + "seconds.");
System.out.println(latest.getDate());
for (int i = 0; i < latest.getTop_stories().size(); i++) {
System.out.println(latest.getTop_stories().get(i));
}
for (int i = 0; i < latest.getStories().size(); i++) {
System.out.println(latest.getStories().get(i));
}
}
}