Xml文件未正确收到

时间:2012-07-12 20:07:30

标签: java android http-post

我正在使用此代码从给定的URL获取rss文件:

// get the xml file from the given url in a string format
public String getXmlFromUrl(String url) 
{
    String xml = null;
    try 
    {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        xml = EntityUtils.toString(httpEntity);

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // return XML
    return xml;
} 

这是获取xml文件文本的代码:

    String yahoo_url = "http://feeds.finance.yahoo.com/rss/2.0/headline?s=intc&region=US&lang=en-US";
    parser = new XMLParser();
    String text = parser.getXmlFromUrl(yahoo_url);
    System.out.println(text);

而不是给我整个文件,我只得到两行:

"?xml version="1.0" encoding="utf-8"?
rss version="2.0"><channel/></rss?"

可能是什么问题?

由于

1 个答案:

答案 0 :(得分:0)

问题可能是某些编码问题。如果使用错误的字符集处理XML字符串,可能会将某些字符误解为例如新行(将解释样本中的换行符)或End-Of-File。

尝试告诉EntityUtils字符串的字符集(可能是UTF-8):

xml = EntityUtils.toString(httpEntity, "UTF-8");