OKHTTP解析json数据
先上代码例子:
public class OKHTTPdemo {
public static void main(String[] args) throws IOException {
//联网获取数据,返回值为String型的
OKHTTPdemo example = new OKHTTPdemo();
ObjectMapper mapper = new ObjectMapper();
String response = example.run("https://api.douban.com/v2/movie/subject/1764796");
//json数据的解析
JsonNode jsonNode = mapper.readTree(response);
String max = jsonNode.path("rating").path("average").asText();
System.out.println(max);
}
private String run(String url) throws IOException {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
}
}
注意的是:需要build path添加开源框架OKHTTP.jar和okio.jar