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

回答編集履歴

1

gzip解析方法を追記

2021/07/11 01:45

投稿

meex
meex

スコア83

answer CHANGED
@@ -39,4 +39,81 @@
39
39
  https://qiita.com/miyatin0212/items/fdfe3c6141323ae281c3
40
40
  ⇒import play.libs.Json; JsonNode jsonNode = Json.parse(jsonStr);
41
41
 
42
- を使うことにより解決できました。
42
+ を使うことにより解決できました。
43
+
44
+
45
+ ---
46
+
47
+ なお、KeepaのGZIPレスポンスを解析するときは「new GZIPInputStream(is,100);」
48
+ を使うとうまくいきました。
49
+
50
+ ```java
51
+ HttpURLConnection urlConn = null;
52
+ InputStream is = null;
53
+ GZIPInputStream in = null;
54
+ BufferedReader reader = null;
55
+
56
+ StringBuilder output = new StringBuilder();
57
+
58
+ try {
59
+ //接続するURLを指定する
60
+ URL url = new URL(strUrl);
61
+
62
+ //コネクションを取得する
63
+ urlConn = (HttpURLConnection) url.openConnection();
64
+ urlConn.setRequestMethod("GET");
65
+ urlConn.setRequestProperty("Accept-Encoding", "gzip");
66
+ urlConn.setRequestProperty("Accept", "gzip, deflate");
67
+ urlConn.setRequestProperty("Content-Encoding", "gzip");
68
+ urlConn.setRequestProperty("Connection", "Keep-Alive");
69
+ urlConn.setRequestProperty("Accept-Language", "jp");
70
+ urlConn.setRequestProperty("User-Agent","Mozilla/5.0 (Macintosh; U; Intel Mac OS X; ja-JP-mac; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
71
+
72
+ urlConn.connect();
73
+
74
+ int status = urlConn.getResponseCode();
75
+
76
+ System.out.println("HTTPstatus: " + status);
77
+
78
+ if (status == HttpURLConnection.HTTP_OK) {
79
+
80
+ is = urlConn.getInputStream();
81
+
82
+ in = new GZIPInputStream(is,100);
83
+
84
+ reader = new BufferedReader(new InputStreamReader(in));
85
+
86
+ String line;
87
+
88
+ while ((line = reader.readLine()) != null) {
89
+ System.out.println(222);
90
+ output.append(line);
91
+ // JSONObject myjson = new JSONObject(line);
92
+ // JSONArray the_json_array = myjson.getJSONArray("profiles");
93
+
94
+ }
95
+
96
+ System.out.println(output.toString());
97
+ }
98
+
99
+ } catch (IOException e) {
100
+ e.printStackTrace();
101
+ } finally {
102
+ try {
103
+ if (reader != null) {
104
+ reader.close();
105
+ }
106
+ if (urlConn != null) {
107
+ urlConn.disconnect();
108
+ }
109
+ } catch (IOException e) {
110
+ e.printStackTrace();
111
+ }
112
+ }
113
+ return
114
+ //"OK"
115
+ output.toString()
116
+ ;
117
+ }
118
+ ```
119
+ ![イメージ説明](a405cdd35508f991e084a900968bdf7a.png)