版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010741376/article/details/45055945
通过调用笑话列表API获取笑话列表
先写好封装json的实体类:
package com.web.entity;
import java.util.List;
public class Joke {
private String status;
private String desc;
private List<jokeSon> detail;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public List<jokeSon> getDetail() {
return detail;
}
public void setDetail(List<jokeSon> detail) {
this.detail = detail;
}
}
package com.web.entity;
public class jokeSon {
private int id;
private String xhid;
private String author;
private String content;
private String picUrl;
private String status;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getXhid() {
return xhid;
}
public void setXhid(String xhid) {
this.xhid = xhid;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getPicUrl() {
return picUrl;
}
public void setPicUrl(String picUrl) {
this.picUrl = picUrl;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
核心类:
package com.web.util;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import com.google.gson.Gson;
import com.web.entity.Joke;
import com.web.entity.jokeSon;
public class JokeUtil {
/**
* 发送http请求获取返回接口
*/
public static String httpRequest(String requestUrl){
StringBuffer sb=new StringBuffer();
try {
URL url=new URL(requestUrl);
HttpURLConnection httpUrlConn=(HttpURLConnection)url.openConnection();
httpUrlConn.setDoInput(true);
httpUrlConn.setUseCaches(false);
httpUrlConn.setRequestMethod("GET");
httpUrlConn.connect();
/**
* 将返回的输入流转换成字符串
*/
InputStream inputStream=httpUrlConn.getInputStream();
InputStreamReader inputReader=new InputStreamReader(inputStream,"utf-8");
BufferedReader reader=new BufferedReader(inputReader);
String str=null;
while((str=reader.readLine())!=null){
sb.append(str);
}
reader.close();
inputReader.close();
inputStream.close();
inputStream=null;
httpUrlConn.disconnect();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sb.toString();
}
/**
* 获取笑话集
*/
public static List<jokeSon> getJoke(){
String requestUrl="http://api.1-blog.com/biz/bizserver/xiaohua/list.do?maxXhid=15602&minXhid=10000&size=5";
StringBuffer sb=new StringBuffer();
String json=httpRequest(requestUrl);
Gson gson=new Gson();
Joke joke=gson.fromJson(json,Joke.class);
return (List<jokeSon>)joke.getDetail();
}
public static void main(String[] args) {
List<jokeSon> list=getJoke();
for(jokeSon jok:list){
System.out.println(jok.getAuthor()+"\n"+jok.getContent()+"\n");
}
}
}
获取的效果: