问题描述
我想从这个URL简单的HTTP响应:http://realtorsipad.demowebsiteonline.net/eventsfeed.php
但意外的是没有返回,而另一返回的HTML页面预期的XML响应!
我不能够理解什么是问题。
下面是示例活动:
公共类MainActivity1延伸活动{
字符串parsingWebURL =http://realtorsipad.demowebsiteonline.net/eventsfeed.php;
串线=;
文档docXML; / **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
行=的getXML();
的System.out.println(线);
} // ------------------------------------------------
公共字符串的getXML(){
串strXML =;
尝试{
DefaultHttpClient的HttpClient =新DefaultHttpClient();
HttpPost httpPost =新HttpPost(parsingWebURL);
HTT presponse HTT presponse = httpClient.execute(httpPost);
HttpEntity httpEntity = HTT presponse.getEntity();
strXML = EntityUtils.toString(httpEntity);
返回strXML;
}赶上(例外E1){
e1.printStackTrace();
}
返回strXML;
}
}
这不是你的code本身,它的网站,它有很多的重定向响应请求时使用移动用户代理进行。
要复制一个桌面浏览器,改变你的用户代理字符串。像这样:
公共字符串的getXML(){
串strXML =;
尝试{ 最终的HttpParams PARAMS =新BasicHttpParams();
HttpClientParams.setRedirecting(参数,可以真正的);
HttpClientParams.setCookiePolicy(参数,可以CookiePolicy.BROWSER_COMPATIBILITY); DefaultHttpClient的HttpClient =新DefaultHttpClient(PARAMS);
httpClient.getParams()的setParameter(CoreProtocolPNames.USER_AGENT,Mozilla的/ 5.0(Windows NT的6.1; Win64的; 64; RV:23.0)的Gecko / 20131011火狐/ 23.0)。 HTTPGET HTTPGET =新HTTPGET(parsingWebURL); HTT presponse HTT presponse = httpClient.execute(HTTPGET);
HttpEntity httpEntity = HTT presponse.getEntity();
strXML = EntityUtils.toString(httpEntity);
返回strXML;
}赶上(例外E1){
e1.printStackTrace();
}
返回strXML;
}
I am trying to get simple HTTP response from this URL : http://realtorsipad.demowebsiteonline.net/eventsfeed.php
But surprisingly it not returning expected XML response rather returning another HTML page!
I am not being able understand what is issue.
Here is sample activity :
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;
}
}
It's not your code per se, it's the site, it responds with a lot of redirects when the request is made with a mobile user-agent.
To replicate a desktop browser, change your user agent string. Like so:
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;
}
这篇关于为什么我得到不同的Htt presponse比Android的浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!