new Thread(){
@Override
public void run() {
OkHttpClient OkHttpClient = new OkHttpClient();
Request Request = new Request.Builder().url("http://192.168.1.1:8080/json.txt").get().build();
Call calls = OkHttpClient.newCall(Request);
try {
Response execute = calls.execute();
Gson Gson=new Gson();
GsonBean gsonBean = Gson.fromJson(execute.body().string(), GsonBean.class);
System.out.println("name= "+gsonBean.getName()+" Title= "+gsonBean.getStories().get(0).getTitle());
} catch (IOException e) {
e.printStackTrace();
} }
}.start();
上面有用到okHttp
GsonBean.java
public class GsonBean {
private String name;
private int timestamp;
private List<StoriesBean> stories;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getTimestamp() {
return timestamp;
}
public void setTimestamp(int timestamp) {
this.timestamp = timestamp;
}
public List<StoriesBean> getStories() {
return stories;
}
public void setStories(List<StoriesBean> stories) {
this.stories = stories;
}
public static class StoriesBean {
private String date;
private String display_date;
private int id;
private String title;
private List<String> images;
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getDisplay_date() {
return display_date;
}
public void setDisplay_date(String display_date) {
this.display_date = display_date;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public List<String> getImages() {
return images;
}
public void setImages(List<String> images) {
this.images = images;
}
}
}
{
"name": "",
"stories": [
{
"date": "20140428",
"display_date": "4 月 28 日",
"id": 3879106,
"images": [
"http://a/c.jpg"
],
"title": "深夜食堂 · 如何在当代中国活到 70 岁"
},
{
"date": "20140427",
"display_date": "4 月 27 日",
"id": 3876052,
"images": [
"http://a/b.jpg"
],
"title": "深夜食堂 · 不必惶恐"
}
],
"timestamp": 1397052001
}