回答編集履歴

3

さらにサンプルに合わせた

2023/05/17 09:56

投稿

jimbe
jimbe

スコア12659

test CHANGED
@@ -7,14 +7,10 @@
7
7
  import java.nio.charset.StandardCharsets;
8
8
 
9
9
  public class Sample12 {
10
- public static void main(String[] args) throws Exception {
11
- String response = post("東京ミッドタウンから国立新美術館まで歩いて5分で着きます。");
10
+ private static final String APPID = "dj00aiZpPTlFbHhqcnVzRWk3byZzPWNvbnN1bWVyc2VjcmV0Jng9N2M-";
12
- System.out.println(response);
11
+ private static final String APIURL = "https://jlp.yahooapis.jp/KeyphraseService/V2/extract";
13
- }
14
12
 
15
- private static String post(String query) throws IOException {
13
+ public static String post(String query) throws IOException {
16
- String APPID = "dj00aiZpPTlFbHhqcnVzRWk3byZzPWNvbnN1bWVyc2VjcmV0Jng9N2M-";
17
- URL url = new URL("https://jlp.yahooapis.jp/KeyphraseService/V2/extract");
18
14
  String json =
19
15
  "{\r\n"
20
16
  + " \"id\": \"1234-1\",\r\n"
@@ -25,7 +21,7 @@
25
21
  + " }\r\n"
26
22
  + "}";
27
23
 
28
- URLConnection conn = url.openConnection();
24
+ URLConnection conn = new URL(APIURL).openConnection();
29
25
  conn.setDoOutput(true);
30
26
  conn.setRequestProperty("Content-Type", "application/json");
31
27
  conn.setRequestProperty("User-Agent", "Yahoo AppID: " + APPID);
@@ -41,6 +37,11 @@
41
37
  }
42
38
  return baos.toString(StandardCharsets.UTF_8);
43
39
  }
40
+
41
+ public static void main(String[] args) throws Exception {
42
+ String response = post("東京ミッドタウンから国立新美術館まで歩いて5分で着きます。");
43
+ System.out.println(response);
44
+ }
44
45
  }
45
46
  ```
46
47
  ```

2

サンプルに形を合わせてみた

2023/05/17 09:43

投稿

jimbe
jimbe

スコア12659

test CHANGED
@@ -6,8 +6,14 @@
6
6
  import java.net.URLConnection;
7
7
  import java.nio.charset.StandardCharsets;
8
8
 
9
- public class YahooTestDemo {
9
+ public class Sample12 {
10
10
  public static void main(String[] args) throws Exception {
11
+ String response = post("東京ミッドタウンから国立新美術館まで歩いて5分で着きます。");
12
+ System.out.println(response);
13
+ }
14
+
15
+ private static String post(String query) throws IOException {
16
+ String APPID = "dj00aiZpPTlFbHhqcnVzRWk3byZzPWNvbnN1bWVyc2VjcmV0Jng9N2M-";
11
17
  URL url = new URL("https://jlp.yahooapis.jp/KeyphraseService/V2/extract");
12
18
  String json =
13
19
  "{\r\n"
@@ -15,22 +21,25 @@
15
21
  + " \"jsonrpc\" : \"2.0\",\r\n"
16
22
  + " \"method\" : \"jlp.keyphraseservice.extract\",\r\n"
17
23
  + " \"params\" : {\r\n"
18
- + " \"q\" : \"東京ミッドタウンから国立新美術館まで歩いて5分で着きます。\"\r\n"
24
+ + " \"q\" : \"" + query + "\"\r\n"
19
25
  + " }\r\n"
20
26
  + "}";
21
27
 
22
28
  URLConnection conn = url.openConnection();
23
29
  conn.setDoOutput(true);
24
30
  conn.setRequestProperty("Content-Type", "application/json");
25
- conn.setRequestProperty("User-Agent", "Yahoo AppID: dj00aiZpPTlFbHhqcnVzRWk3byZzPWNvbnN1bWVyc2VjcmV0Jng9N2M-");
31
+ conn.setRequestProperty("User-Agent", "Yahoo AppID: " + APPID);
26
32
 
27
33
  try(OutputStream out = conn.getOutputStream()) {
28
34
  out.write(json.getBytes(StandardCharsets.UTF_8));
29
35
  }
30
36
 
37
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
31
- try(BufferedReader r = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
38
+ try(InputStream in = conn.getInputStream()) {
39
+ byte[] buf = new byte[4096];
32
- for(String line; (line=r.readLine())!= null; ) System.out.println(line);
40
+ for(int n; (n=in.read(buf))>=0; ) baos.write(buf, 0, n);
33
41
  }
42
+ return baos.toString(StandardCharsets.UTF_8);
34
43
  }
35
44
  }
36
45
  ```

1

Content-Length 削除

2023/05/17 09:19

投稿

jimbe
jimbe

スコア12659

test CHANGED
@@ -22,7 +22,6 @@
22
22
  URLConnection conn = url.openConnection();
23
23
  conn.setDoOutput(true);
24
24
  conn.setRequestProperty("Content-Type", "application/json");
25
- conn.setRequestProperty("Content-Length", ""+json.length());
26
25
  conn.setRequestProperty("User-Agent", "Yahoo AppID: dj00aiZpPTlFbHhqcnVzRWk3byZzPWNvbnN1bWVyc2VjcmV0Jng9N2M-");
27
26
 
28
27
  try(OutputStream out = conn.getOutputStream()) {