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<String, String> params, String charset, boolean pretty) {
StringBuffer response = new StringBuffer();
HttpClient client = new HttpClient();
HttpMethod method = new PostMethod(url);
if (params != null) {
HttpMethodParams p = new HttpMethodParams();
for (Map.Entry<String, String> entry : params.entrySet()) {
p.setParameter(entry.getKey(), entry.getValue());
}
method.setParams(p);
}
try {
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 (IOException e) {
log.error("HTTP Post " + url + " 失败", e);
} finally {
method.releaseConnection();
}
return response.toString();
}
//解析json数据
public static Map<String, String> toMap(String jsonString) throws JSONException {
JSONObject jsonObject = new JSONObject(jsonString);
Map<String, String> result = new HashMap<String, String>();
Iterator<?> iterator = jsonObject.keys();
String key = null;
String value = null;
while (iterator.hasNext()) {
key = (String) iterator.next();
value = jsonObject.getString(key);
result.put(key, value);
}
return result;
}
//获取书籍信息
public static void bookinfo(String uri) {
String y = doGet(uri, null, "UTF-8", true);
y=y.replace("\"", "'");
y=y.replaceAll("\\\\","/");
y=y.replaceAll("//","/");
Map<String, String> map=null;
try {
map = toMap(y);
} catch (JSONException e) {
System.out.println("失败");
e.printStackTrace();
}
iteator(map);
print(map);
}
public static void iteator(Map<String, String> map){
Iterator<?> iter = map.entrySet().iterator();
String key,value;
while(iter.hasNext()) {
Map.Entry entry = (Map.Entry)iter.next();
key = (String)entry.getKey();
value = (String)entry.getValue();
System.out.println(key+" "+value);
}
}
//根据需要,输出书籍信息
public static void print(Map<String, String> map){
System.out.println("***********************");
System.out.println("标题: "+map.get("title"));
String author=map.get("author").toString();
author=author.replace("[", "");
author=author.replace("]", "");
author=author.replace("\"", "");
System.out.println("作者: "+author);
System.out.println("价格: "+map.get("price"));
System.out.println("出版社: "+map.get("publisher"));
System.out.println("出版日期: "+map.get("pubdate"));
System.out.println("ISBN13: "+map.get("isbn13"));
Map<String, String> map2=null;
try {
map2=toMap(map.get("images").toString().replace("\"", "'"));
} catch (JSONException e) {
e.printStackTrace();
}
System.out.println("封面图片(小): "+map2.get("small"));
System.out.println("封面图片(中): "+map2.get("medium"));
System.out.println("封面图片(大): "+map2.get("large"));
}
public static void main(String[] args) {
String isbn="9787302423287";
String uri="https://api.douban.com/v2/book/isbn/"+isbn;
bookinfo(uri);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
GetBook.zip (17个子文件)
GetBook
bin
HttpTookit.class 8KB
GetBook.iml 484B
.settings
org.eclipse.jdt.core.prefs 598B
src
HttpTookit.java 7KB
.idea
misc.xml 2KB
copyright
profiles_settings.xml 76B
modules.xml 254B
compiler.xml 686B
workspace.xml 32KB
libraries
lib.xml 352B
.project 383B
.classpath 552B
out
production
GetBook
HttpTookit.class 8KB
lib
commons-codec-1.10.jar 278KB
commons-httpclient-3.1.jar 298KB
json.jar 85KB
commons-logging-1.2.jar 60KB
共 17 条
- 1
资源评论
- pauleric2018-04-18可以用,多谢
- 兮于徐2022-03-21豆瓣api已经不可以用了,可以试试这个 https://jike.xyz/jiekou/isbn.html
NovenBae
- 粉丝: 179
- 资源: 29
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功