字节流输出“utf-8”utf-8的字符流
@Override
public String doubanBookAPI(String title) throws Exception {
String modifiedTitle = URLEncoder.encode(title);
String searchUrl = "https://api.douban.com/v2/book/search?q=" + modifiedTitle + "&count=1";
//initialise
URL f_url = null;
HttpURLConnection conn = null;
String bookInfo = "";
try {
//Making HTTP request
f_url = new URL(searchUrl);
//open the connection
conn = (HttpURLConnection) f_url.openConnection();
conn.setDoOutput(false);
conn.setDoInput(true);
conn.setUseCaches(false);
//t the connection method to GET
conn.setRequestMethod("GET");
conn.connect();
Read the response
BufferedReader in = null;
in = new BufferedReader(new InputStreamReader(
conn.getInputStream(), "utf-8"));
String line;
while ((line = in.readLine()) != null) {
bookInfo += line;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
conn.disconnect();
}
return bookInfo;
}