当前位置:网站首页 > Java基础 > 正文

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

}

  • 上一篇: java1-3基础入门
  • 下一篇: java语法基础75
  • 版权声明


    相关文章:

  • java1-3基础入门2024-10-25 08:18:02
  • 小学基础能学java吗2024-10-25 08:18:02
  • 程序设计基础c语言和java2024-10-25 08:18:02
  • java基础演讲讲什么简单易懂2024-10-25 08:18:02
  • 数据结构和算法基础java2024-10-25 08:18:02
  • java语法基础752024-10-25 08:18:02
  • java基础接口类2024-10-25 08:18:02
  • java 基础 知乎2024-10-25 08:18:02
  • java零基础班和和就业班2024-10-25 08:18:02
  • java0基础动力节点2024-10-25 08:18:02