12

我正在尝试从此 URL 获取简单的 HTTP 响应:http ://realtorsipad.demowebsiteonline.net/eventsfeed.php

但令人惊讶的是,它没有返回预期的 XML 响应,而是返回另一个 HTML 页面!

我无法理解什么是问题。

这是示例活动:

public class MainActivity1 extends Activity {
    String parsingWebURL = "http://realtorsipad.demowebsiteonline.net/eventsfeed.php";
    String line = "";
    Document docXML;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        line = getXML();
        System.out.println(line);
    }

    // ------------------------------------------------
    public String getXML() {
        String strXML = "";
        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(parsingWebURL);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            strXML = EntityUtils.toString(httpEntity);
            return strXML;
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return strXML;
    }
}
4

1 回答 1

7

这不是您的代码本身,而是网站,当使用移动用户代理发出请求时,它会响应大量重定向。

要复制桌面浏览器,请更改您的用户代理字符串。像这样:

public String getXML() {
    String strXML = "";
    try {

        final HttpParams params = new BasicHttpParams();
        HttpClientParams.setRedirecting(params, true);
        HttpClientParams.setCookiePolicy(params, CookiePolicy.BROWSER_COMPATIBILITY);

        DefaultHttpClient httpClient = new DefaultHttpClient(params);
        httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:23.0) Gecko/20131011 Firefox/23.0");

        HttpGet httpGet = new HttpGet(parsingWebURL);

        HttpResponse httpResponse = httpClient.execute(httpGet);
        HttpEntity httpEntity = httpResponse.getEntity();
        strXML = EntityUtils.toString(httpEntity);
        return strXML;
    } catch (Exception e1) {
        e1.printStackTrace();
    }
    return strXML;
}
于 2013-06-15T07:23:56.603 回答