httpclient返回乱码

时间:2011-11-23 21:47:25

标签: android httpclient

我正在尝试使用HTTPClient获取页面,但它似乎会返回乱码(我认为二进制)。

这是我的代码:

public Boolean getPage(String url, String referer) {
    httpClient.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_1);
    httpClient.getParams().setParameter("http.socket.timeout", new Integer(1000));
    httpClient.getParams().setParameter("http.protocol.content-charset", "UTF-8");

    HttpGet httpGet = new HttpGet(url);
    response = null;

    httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3");
    httpGet.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    httpGet.setHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
    httpGet.setHeader("Accept-Encoding", "gzip,deflate");
    httpGet.setHeader("Referer", referer);

    int tryNumber = 0;

    while(tryNumber<5){
        tryNumber++;
        try {
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            ret = httpClient.execute(httpGet,responseHandler).toString();
            Log.v("Info:", ret);
        }
        catch(Exception e) {
            error = e;
            break;
        }
    }
}

有谁可以指出我哪里出错?

我正在尝试获取此页面的内容:http://hosh.me.uk/test.php

查看屏幕截图:http://img.ctrlv.in/4ecd69c40a590.jpg

Hosh

1 个答案:

答案 0 :(得分:4)

只是一个猜测,但可能是响应被压缩了吗?也许您应该删除以下行,以便您的回复是纯文本。

httpGet.setHeader("Accept-Encoding", "gzip,deflate");