从URL下载会导致二进制响应

时间:2012-04-08 12:01:36

标签: android

我尝试从此网址获取字符串:
http://autoc.finance.yahoo.com/autoc?query=google&callback=YAHOO.Finance.SymbolSuggest.ssCallback

当我在Chrome中查看此内容时,我看到正确的JavaScript响应,但是当从具有相同标头的应用程序下载时,我得到二进制响应。如何在应用程序中获得相同的正确响应?

修改 代码:

public RESTResponse<T> get(String url) {
    HttpGet get = new HttpGet(url);
    setHeaders(get);
    return execute(get);
}                                                         

private void setHeaders(HttpRequestBase request) {
    request.setHeader("Accept", "text/html,application/xhtml+xml,application/xml");
    request.setHeader("Accept-Encoding", "gzip,deflate,sdch");
    request.setHeader("Accept-Charset", "UTF-8");
}

private RESTResponse<T> execute(HttpRequestBase request) {
    DefaultHttpClient client = new DefaultHttpClient();
    try {
        HttpResponse response = client.execute(request);
        return responseFactory.create(response);
    } catch (IOException e) {
        throw new RuntimeException("Can't perform request.", e);
    }
}

1 个答案:

答案 0 :(得分:1)

如果没有看到你的代码,很难说出问题是什么 - 但你可以使用那个:

https://github.com/ligi/LigiAndroidCommons/blob/master/src/org/ligi/android/common/net/NetHelper.java