• 大小: 661KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-05-14
  • 语言: Java
  • 标签: Java代码  

资源简介

使用豆瓣API,根据书籍ISBN码,获取该本书籍的详细信息

资源截图

代码片段和文件信息

import org.apache.commons.httpclient.HttpClient;  
import org.apache.commons.httpclient.HttpMethod;  
import org.apache.commons.httpclient.HttpStatus;  
import org.apache.commons.httpclient.URIException;  
import org.apache.commons.httpclient.methods.GetMethod;  
import org.apache.commons.httpclient.methods.PostMethod;  
import org.apache.commons.httpclient.params.HttpMethodParams;  
import org.apache.commons.httpclient.util.URIUtil;  
import org.apache.commons.logging.Log;  
import org.apache.commons.logging.LogFactory;  
import org.json.JSONException;  
import org.json.JSONobject;  
  
import java.io.BufferedReader;  
import java.io.IOException;  
import java.io.InputStreamReader;  
import java.util.HashMap;  
import java.util.Iterator;  
import java.util.Map;  

public final class HttpTookit {  
        private static Log log = LogFactory.getLog(HttpTookit.class);
        
        //Http Get
        public static String doGet(String url String queryString String charset boolean pretty) {  
                StringBuffer response = new StringBuffer();  
                HttpClient client = new HttpClient();  
                HttpMethod method = new GetMethod(url);  
                try {  
                        if (queryString!=null)
                                method.setQueryString(URIUtil.encodeQuery(queryString));  
                        client.executeMethod(method);  
                        if (method.getStatusCode() == HttpStatus.SC_OK) {  
                                BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream() charset));  
                                String line;  
                                while ((line = reader.readLine()) != null) {  
                                        if (pretty)  
                                                response.append(line).append(System.getProperty(“line.separator“));  
                                        else  
                                                response.append(line);  
                                }  
                                reader.close();  
                        }  
                } catch (URIException e) {  
                        log.error(“HTTP Get “ + queryString + “ 失败“ e);
                } catch (IOException e) {  
                        log.error(“HTTP Get “ + queryString + “ 失败“ e);
                } finally {  
                        method.releaseConnection();  
                }  
                return response.toString();  
        }

        //Http Post
        public static String doPost(String url Map params String charset boolean pretty) {  
                StringBuffer response = new StringBuffer();  
                HttpClient client = new HttpClient();  
                HttpMethod method = new PostMethod(url);
                if (params != null) {  
                        HttpMethodParams 

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2016-12-15 10:02  GetBook\
     文件         552  2016-10-23 19:38  GetBook\.classpath
     目录           0  2017-01-08 17:01  GetBook\.idea\
     文件         686  2016-10-23 19:31  GetBook\.idea\compiler.xml
     目录           0  2016-12-15 10:02  GetBook\.idea\copyright\
     文件          76  2016-10-23 19:31  GetBook\.idea\copyright\profiles_settings.xml
     目录           0  2016-12-15 10:02  GetBook\.idea\libraries\
     文件         352  2016-10-23 19:31  GetBook\.idea\libraries\lib.xml
     文件        2368  2017-01-08 17:01  GetBook\.idea\misc.xml
     文件         254  2016-11-15 14:42  GetBook\.idea\modules.xml
     文件       32262  2017-01-08 17:01  GetBook\.idea\workspace.xml
     文件         383  2016-10-23 18:55  GetBook\.project
     目录           0  2016-12-15 10:02  GetBook\.settings\
     文件         598  2016-10-23 18:55  GetBook\.settings\org.eclipse.jdt.core.prefs
     文件         484  2016-10-23 19:31  GetBook\GetBook.iml
     目录           0  2017-01-08 17:03  GetBook\bin\
     文件        7787  2017-01-08 17:09  GetBook\bin\HttpTookit.class
     目录           0  2016-12-15 10:02  GetBook\lib\
     文件      284184  2016-10-23 19:37  GetBook\lib\commons-codec-1.10.jar
     文件      305001  2016-10-23 19:20  GetBook\lib\commons-httpclient-3.1.jar
     文件       61829  2016-10-23 19:21  GetBook\lib\commons-logging-1.2.jar
     文件       86587  2016-10-23 19:30  GetBook\lib\json.jar
     目录           0  2016-12-15 10:02  GetBook\out\
     目录           0  2016-12-15 10:02  GetBook\out\production\
     目录           0  2017-01-08 16:57  GetBook\out\production\GetBook\
     文件        7720  2017-01-08 16:57  GetBook\out\production\GetBook\HttpTookit.class
     目录           0  2017-01-08 16:57  GetBook\src\
     文件        7649  2017-01-08 17:09  GetBook\src\HttpTookit.java

评论

共有 条评论

相关资源