通过调用文摘列表API获取文摘

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010741376/article/details/45055657

通过调用文摘列表API获取文摘:

调用后返回收藏的文摘列表,包括散文,奇葩,历史,经济四种分类文章

地址:http://api.1-blog.com/biz/bizserver/article/list.do

请求参数:page:分页,第几页,默认为1,size:分页的大小,默认为10;type:要获取的文章类型,不传为全部分类

返回结果:

 

{    
    "status": "000000",        ----返回状态,六个0表示成功    
    "desc": null,              ----返回结果描述,六个0表示成功    
    "detail": [                ----具体文章列表,是一个数组    
        {    
            "title":"清朝商业",        ----文章标题    
            "article_url": "http://xxxx",   ---文章地址  
            "my_abstract": "xxx",   ----笑话摘要  
            "article_type": "2",  ----文章类型,0: 散文 1 :奇葩 2:经济3:野史       
        },    
        {    
            "title":"孙中山:以为真正的爱国者",        ----文章标题    
            "article_url": "http://xxxx",   ----文章地址  
            "my_abstract": "xxx",   ----笑话摘要   
            "article_type": "3",  ----文章类型,0: 散文 1 :奇葩 2:经济3:野史       
        }    
    ]    
} 




实体类:

package com.web.entity;

import java.util.List;


/**
 * 文摘实体类
 * @author Administrator
 *
 */
public class Essay {
    private String status;
    private String desc;
    private List<SaySon> 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<SaySon> getDetail() {
        return detail;
    }
    public void setDetail(List<SaySon> detail) {
        this.detail = detail;
    }
    
    
    
}

package com.web.entity;

public class SaySon {
  private String title;
  private String article_url;
  public String getTitle() {
	return title;
}
public void setTitle(String title) {
	this.title = title;
}
public String getArticle_url() {
	return article_url;
}
public void setArticle_url(String articleUrl) {
	article_url = articleUrl;
}
public String getMy_abstract() {
	return my_abstract;
}
public void setMy_abstract(String myAbstract) {
	my_abstract = myAbstract;
}
public String getArticle_type() {
	return article_type;
}
public void setArticle_type(String articleType) {
	article_type = articleType;
}
private String my_abstract;
  private String article_type;
  
  
}

核心类:

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.List;

import com.google.gson.Gson;
import com.web.entity.Essay;
import com.web.entity.SaySon;


/**
 * 获取文摘工具类
 * @author Administrator
 *
 */
public class AbstractUtil {
           /**
            * 发送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 inputStreamReader=new InputStreamReader(inputStream,"utf-8");
		    BufferedReader reader=new BufferedReader(inputStreamReader);
		    
		    String line=null;
		    while((line=reader.readLine())!=null){
		    	sb.append(line);
		    }
		    /**
		     * 释放资源
		     */
		    reader.close();
		    inputStreamReader.close();
		    inputStream.close();
		    inputStream=null;
		    httpUrlConn.disconnect();
		    	
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
          return sb.toString();
		
	}
	
	/**
	 * 获取文摘内容
	 */
	   public static List<SaySon> getAbstract(){
		 String requestUrl="http://api.1-blog.com/biz/bizserver/article/list.do?size=5&page=1";
		 String json=httpRequest(requestUrl);   
		 Gson gson=new Gson();
		 Essay essay=gson.fromJson(json,Essay.class);
		   return  essay.getDetail();
	   }
	   
	   public static void main(String[] args) {
		     List<SaySon> s=getAbstract();
		     for(SaySon say:s){
		    	 System.out.println(say.getTitle()+"-"+say.getArticle_type()+"-"+say.getArticle_url()+"-"+say.getMy_abstract());
		     }
		     
		   
		   
	}
	
}
结果显示:

通过调用文摘列表API获取文摘

上一篇:集成spring3,hibernate4


下一篇:Hibernate分页