Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.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 为什么使用DefaultHTTPClient获取页面时会出现乱码?_Java_Android - Fatal编程技术网

Java 为什么使用DefaultHTTPClient获取页面时会出现乱码?

Java 为什么使用DefaultHTTPClient获取页面时会出现乱码?,java,android,Java,Android,我正在尝试使用Android的DefaultHTTPClient获取一个页面,并使用Jsoup对其进行解析。我得到了一个非常奇怪的响应,和标记中的所有HTML都被编码成了某种东西 <html> <head></head> <body> ��������������Y�#I�&amp;�\�+��*;����/U���53�*��U�=�D�I:I� ����X�����΃��=H��2�`Ѓ ��o��nͽ�C瘹;�

我正在尝试使用Android的
DefaultHTTPClient
获取一个页面,并使用Jsoup对其进行解析。我得到了一个非常奇怪的响应,
标记中的所有HTML都被编码成了某种东西

<html>
  <head></head>
  <body>
       ��������������Y�#I�&amp;�\�+��*;����/U���53�*��U�=�D�I:I� ����X�����΃��=H��2�`Ѓ  ��o��nͽ�C瘹;�l2Y�I_l�����;f��W�k��o2.����?�r&gt;��œ�qYξ&lt;&lt;&lt;;;�g*��ѡl���9&gt;s@I��`R��V �c�������Ɂ��e�����,&gt; }���A�����W�?��&quot;.��ˡhޖ�Qy1�oL�_�W�h?9�E?Ofe��KO�Q��(�Av�N�h@��G�qvV�_G��W�g�'q�2�N��L�?�&quot;鳷�x�o�����$9�}/;'#ȸ Q��&amp;�2�\�a��aǔ�L�I�ԯ�=���TPFE� ���:�,�H�N�'QQԯ&lt;&gt;�i}�x��'$�'O ��qy@J�h 2��ᓃ�CH��ʤO���0�LD)��p8�챺)
  </body>
</html>

��������������Y�#我�&;�\�+��*;����/U���53�*��U�=�D�I:我� ����X�����΃��=H��2.�`Ѓ  ��o��nͽ�C瘹;�l2Y�我�����;F��W�K��氧气。����?�R��œ�qYξ;;�g*��ѡl���9s@I��`R��v�C�������Ɂ��E�����, }���A.�����W�?��".��ˡhޖ�Qy1�oL�_�W�h?9�奥菲��击倒对手�Q��(�影音�N�h@��G�qvV�_G��W�G�'Q�2.�N��L�?�"鳷�x�o�����$9�}/;'#ȸQ��&;�2.�\�A.��aǔ�L�我�ԯ�=���TPFE� ���:�,�H�N�'QQԯ�i}�x��'$�'O��qy@J�h2��ᓃ�中国��ʤO���0�(劳工处)��p8�챺)
这是我获取页面的方法

  public String doGet(String strUrl, List<NameValuePair> lstParams) throws Exception {

          String strResponse = null;
          HttpGet htpGet = new HttpGet(strUrl);
          //htpGet.addHeader("Accept-Encoding", "gzip, deflate");
          htpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0.1");
          DefaultHttpClient dhcClient = new DefaultHttpClient();
          PersistentCookieStore pscStore = new PersistentCookieStore(this.objContext);
          dhcClient.setCookieStore(pscStore);
          HttpResponse resResponse = dhcClient.execute(htpGet);
          strResponse = EntityUtils.toString(resResponse.getEntity());
          return strResponse;

  }
publicstringdoget(stringstrurl,List lstParams)抛出异常{
字符串strResponse=null;
HttpGet htpGet=新的HttpGet(strUrl);
//addHeader(“接受编码”、“gzip、deflate”);
htpGet.setHeader(“用户代理”、“Mozilla/5.0(Windows NT 6.1;WOW64;rv:15.0)Gecko/20100101 Firefox/15.0.1”);
DefaultHttpClient dhcClient=新的DefaultHttpClient();
PersistentCookieStore pscStore=新的PersistentCookieStore(this.objContext);
dhcClient.setCookieStore(pscStore);
HttpResponse resResponse=dhcClient.execute(htpGet);
strResponse=EntityUtils.toString(resResponse.getEntity());
返回strResponse;
}
为什么会发生这种情况


如果我使用Jsoup本身获取页面,响应很好。我必须使用
Jsoup.connect(“http://www.kat.ph/)。get()
试试这个方法……结果是一样的吗

URL url = new URL("Your_URL");

InputStream is = url.openStream();   // or url.openConnection();

Scanner scan = new Scanner(is);

while(scan.hasNextLine()){

 System.out.println(scan.nextLine());


 }

}

这是因为响应是gzip处理的。我连接了一个自定义响应拦截器来解压缩响应。就是这样:

class Decompressor implements HttpResponseInterceptor {

    /*
     * @see org.apache.http.HttpResponseInterceptor#process(org.apache.http.
     * HttpResponse, org.apache.http.protocol.HttpContext)
     */
    public void process(HttpResponse hreResponse, HttpContext hctContext)   throws HttpException, IOException {

        HttpEntity entity = hreResponse.getEntity();

        if (entity != null) {

            Header ceheader = entity.getContentEncoding();

            if (ceheader != null) {

                HeaderElement[] codecs = ceheader.getElements();

                for (int i = 0; i < codecs.length; i++) {

                    if (codecs[i].getName().equalsIgnoreCase("gzip")) {

                        hreResponse.setEntity(new HttpEntityWrapper(entity) {

                            @Override
                            public InputStream getContent() throws IOException, IllegalStateException {

                                return new GZIPInputStream(wrappedEntity.getContent());

                            }

                            @Override
                            public long getContentLength() {

                                return -1;

                            }

                        });

                        return;

                    }

                }

            }

        }

    }

}
类解压缩程序实现HttpResponseInterceptor{
/*
*@see org.apache.http.HttpResponseInterceptor#process(org.apache.http。
*HttpResponse,org.apache.http.protocol.HttpContext)
*/
公共无效进程(HttpResponse hreResponse,HttpContext hctContext)引发HttpException,IOException{
HttpEntity entity=hresponse.getEntity();
如果(实体!=null){
Header ceheader=entity.getContentEncoding();
如果(ceheader!=null){
HeaderElement[]codecs=ceheader.getElements();
对于(int i=0;i