我试图从这个网址获取Json:https://meduza.io/api/v3/search?chrono=news&page=0&per_page=10&locale=ru,代码非常简单:
public Boolean getData(String apiUrl) {
try {
//disable SSLCertificateValidation
SSLCertificateValidation.disable();
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/json;charset=UTF-8");
headers.add("Accept", "*/*");
ResponseEntity<String> responseEntity = restTemplate.getForEntity(apiUrl, String.class, headers);
if (responseEntity.getStatusCode().toString().equals("200")) {
theLogger.info("Server successfully answered with response code: {} - {}", responseEntity.getStatusCode().toString(), responseEntity.getBody());
return true;
} else {
theLogger.error("Something goes wrong! Server answered with response code: {} and error: {}", responseEntity.getStatusCode().toString(), responseEntity.getBody());
return false;
}
} catch (Exception theException) {
theException.printStackTrace();
theLogger.error("Exception: " + theException);
return false;
}
}
它有效,但我在体内看到了这一点:
Server successfully answered with response code: 200 - ��������Y�n�}B� DiF�d�@P�(v����E�]�J�5CNH��b�<Fh_.O�s93��*v]���cs�s/��!�k'5y.S����ԑ�����Q<�Gq0���9%4/�q2����+��Ϋ�P��~!<�����[�IL[K+���K������J�Eb*�]nV�ݒ;��*U3�y��t���27+iu!5$�Fx�G|����ϰ#a��^T9�x"Ty�Q�V��ܕf)]�Lj%�-���7�PL8'�6��%��~��
����`�T��F;��D���
�F��3��~A����&�+a����xc'X���
��\�
Y�V��5�L
O��0|�fP�Ⱦ<��`@;s��d)�u~�u�9����ngV������ʁ���~�^��F��OM(�l��`E�����^�'G�h��*�cX�E<>�I#,��Q�U���>V&l�4[+��&BkIªF��Ze�~�dnPKe/Z��俉�t���x�j(6�݄b;P�
|cA� ��;�Oի�=$�3:{g��L�G�U��* az���x�`E,��Q��)�̅4%t���f�W�K�$������}�1���Kl�SjV;C�9Ė�DU�oi|��M;��$-@��'�.��GyG���}0w|���|=����3I8����f-0��,������l�[��g��dr�V?.���p<�y�`�m_7x�A�鱯�n��ǿ�k��C+ G�F��J
�&�Nlo8RIq�J@���[\����Y�$�� ��r{X�+��2\�����l���sUȆ���OZ��E�!D�hi�N�L��G��6~-�T����_Azm��s:x��ķ��h��
���[p��1�~ ٭����J���f�����1���k����s�R��{U��`�c��et����=��R�.A��Q���{M�Ee���D���Jq��ROƏ�j���2�~�Lb���~Z��F��{p���T��~E���nG�~X���r�x?|�M�����d����e9�~`�sy/а�v��b���(�����N�x4���o�� ?�F����ew��2��[�Yv��ǣi|�4�n�OEY�]t�+�U(�ƛSv!�M��`}��?ޥB���A)�!�mo#>��s۽x(im��*b�����ī�W�C��a����G$BͻӖ]v�f"N�g�#͆�L�o>yc_���?�
DMtꭘ�T�l�Ӹ��b����LցL�?9��������w����.�.K��J��gV�/�O���鮦����~�ZT��]��a
h�i`��z��;p'��X�U��g��`�&mA�`�<Ws��a��#�C��t�K2t��z",���
����ƛ&�"N`\��C�=`�B8�������oFJ%��
这是什么?我做错了什么?请问有人伸出援助之手吗?
答案 0 :(得分:2)
服务器压缩(gzip)响应。大多数浏览器/工具透明地处理压缩响应。在这里你必须明确地做。
要解压缩您可以使用GZipInputStream的响应。
ResponseEntity<byte[]> response = restTemplate.getForEntity(apiUrl, byte[].class);
GZIPInputStream gzip = new GZIPInputStream(new ByteArrayInputStream(response.getBody()));
// convert the stream to string
StringWriter writer = new StringWriter();
IOUtils.copy(gzip, writer, "UTF-8");
String responseString = writer.toString();
流式转换使用Apache Commons-IO。
服务器似乎总是使用压缩 - 即使客户端没有告诉它(通过发送accept-encoding
标头)。
检查Content-Encoding
标头的值,以测试是否压缩了响应。
response.getHeaders().get("Content-Encoding"); // contains [gzip] if response is compressed