teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

URL のクエリ文字列部分を微修正

2021/10/12 10:49

投稿

jimbe
jimbe

スコア13355

answer CHANGED
@@ -54,7 +54,9 @@
54
54
  }
55
55
 
56
56
  static Document getDocument(String isbn, DocumentBuilder builder) throws IOException, SAXException {
57
+ String operation = "searchRetrieve";
58
+ String query = URLEncoder.encode("isbn=\""+isbn+"\"", "UTF-8");
57
- URL url = new URL("https://iss.ndl.go.jp/api/sru?operation=searchRetrieve&query=isbn%3d%22"+isbn+"%22");
59
+ URL url = new URL("https://iss.ndl.go.jp/api/sru?operation="+operation+"&query="+query);
58
60
  HttpsURLConnection https = (HttpsURLConnection)url.openConnection();
59
61
  https.setRequestMethod("GET");
60
62
  https.connect();

1

cx20 さんのコメントでのご指摘より、コード修正

2021/10/12 10:49

投稿

jimbe
jimbe

スコア13355

answer CHANGED
@@ -23,7 +23,7 @@
23
23
  }
24
24
 
25
25
  static List<Book> getBookList(String isbn) throws ParserConfigurationException, IOException, SAXException, XPathExpressionException {
26
- DocumentBuilderFactory factory = DocumentBuilderFactory.newDefaultInstance();
26
+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
27
27
  //factory.setNamespaceAware(true);
28
28
  DocumentBuilder builder = factory.newDocumentBuilder();
29
29
 
@@ -42,7 +42,7 @@
42
42
 
43
43
  for(int i=0; i<recordDataList.getLength(); i++) {
44
44
  String data = recordDataList.item(i).getTextContent();
45
- Document datadoc = builder.parse(new ByteArrayInputStream(data.getBytes()));
45
+ Document datadoc = builder.parse(new ByteArrayInputStream(data.getBytes("UTF-8")));
46
46
 
47
47
  String title = (String)titleExpr.evaluate(datadoc, XPathConstants.STRING);
48
48
  String creator = (String)creatorExpr.evaluate(datadoc, XPathConstants.STRING);