java基础书籍ISBN码

java基础书籍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.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);

}

}

//根据需要,输出书籍信息

java基础书籍ISBN码 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="87";

String uri="https://api.douban.com/v2/book/isbn/"+isbn;

bookinfo(uri);

}

小讯
上一篇 2025-01-02 14:37
下一篇 2024-12-29 18:24

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/2814.html