Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从HTTP请求中获取正确的数据_Java_Http_Get_Request - Fatal编程技术网

Java 如何从HTTP请求中获取正确的数据

Java 如何从HTTP请求中获取正确的数据,java,http,get,request,Java,Http,Get,Request,我试图使用一个简单的HTTP请求和Java中的get方法从stackoverflow api获取用户信息 我以前使用过这段代码,使用get方法获取另一个HTTP数据,没有问题: URL obj; StringBuffer response = new StringBuffer(); String url = "http://api.stackexchange.com/2.2/users?inname=HCarrasko&site=stackoverflow";

我试图使用一个简单的HTTP请求和Java中的get方法从stackoverflow api获取用户信息

我以前使用过这段代码,使用get方法获取另一个HTTP数据,没有问题:

    URL obj;
    StringBuffer response = new StringBuffer();
    String url = "http://api.stackexchange.com/2.2/users?inname=HCarrasko&site=stackoverflow";
        try {
        obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("GET");
        int responseCode = con.getResponseCode();
        System.out.println("\nSending 'GET' request to URL : " + url);
        System.out.println("Response Code : " + responseCode);
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }

        in.close();
        System.out.println(response.toString());
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
但在这种情况下,当我打印
响应
变量时,我得到的只是一些奇怪的符号,如下所示:

�mRM��0�+�N!���FZq�\�pD�z�:V���JX���M��̛yO^���뾽�g�5J&� �9�YW�%c`do���Y'��nKC38<A�&It�3��6a�,�,]���`/{�D����>6�Ɠ��{��7tF ��E��/����K���#_&�yI�a�v��uw}/�g�5����TkBTķ���U݊c���Q�y$���$�=ۈ��ñ���8f�<*�Amw�W�ـŻ��X$�>'*QN�?�<v�ݠ FH*��Ҏ5����ؔA�z��R��vK���"���@�1��ƭ5��0��R���z�ϗ/�������^?r��&�f��-�OO7���������Gy�B���Rxu�#:0�xͺ}�\�����

�mRM��0�+�N���FZq�\�pD�Z�:v���JX���M��̛y  O^���뾽�G�5J&� �9�YW�%c'do���Y'��nKC386�Ɠ��{��7tF��E��/����K���#_&�彝族�A.�v��uw}/�G�5.����TkBTķ���U݊c���Q�y$���$�=ۈ��ñ���8f�'*QN�?� 内容可能是GZIP编码/压缩的。以下是我在所有使用HTTP的基于Java的客户机应用程序中使用的一般代码片段,旨在解决这个问题:

// Read in the response
// Set up an initial input stream:
InputStream inputStream = fetchAddr.getInputStream(); // fetchAddr is the HttpURLConnection

// Check if inputStream is GZipped
if("gzip".equalsIgnoreCase(fetchAddr.getContentEncoding())){
    // Format is GZIP
    // Replace inputSteam with a GZIP wrapped stream
    inputStream = new GZIPInputStream(inputStream);
}else if("deflate".equalsIgnoreCase(fetchAddr.getContentEncoding())){
    inputStream = new InflaterInputStream(inputStream, new Inflater(true));
} // Else, we assume it to just be plain text

BufferedReader sr = new BufferedReader(new InputStreamReader(inputStream));
String inputLine;
StringBuilder response = new StringBuilder();
// ... and from here forward just read the response...

这依赖于以下导入:
java.util.zip.gzip输入流
java.util.zip.Inflater
;和
java.util.zip.InflateInputStream

内容可能是GZIP编码/压缩的。以下是我在所有使用HTTP的基于Java的客户机应用程序中使用的一般代码片段,旨在解决这个问题:

// Read in the response
// Set up an initial input stream:
InputStream inputStream = fetchAddr.getInputStream(); // fetchAddr is the HttpURLConnection

// Check if inputStream is GZipped
if("gzip".equalsIgnoreCase(fetchAddr.getContentEncoding())){
    // Format is GZIP
    // Replace inputSteam with a GZIP wrapped stream
    inputStream = new GZIPInputStream(inputStream);
}else if("deflate".equalsIgnoreCase(fetchAddr.getContentEncoding())){
    inputStream = new InflaterInputStream(inputStream, new Inflater(true));
} // Else, we assume it to just be plain text

BufferedReader sr = new BufferedReader(new InputStreamReader(inputStream));
String inputLine;
StringBuilder response = new StringBuilder();
// ... and from here forward just read the response...
这依赖于以下导入:
java.util.zip.gzip输入流
java.util.zip.Inflater
;和
java.util.zip.InflateInputStream