各位大哥,我是android的新手,现在写了一个关于http请求的测试代码,我用网页打开请求地址,返回在浏览器上的值是1,并且浏览器中的页面源码是:
<span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Arial,Microsoft Yahei,Simsun,sans-serif'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; ">1</span>因为这个没有head和body,所以我不知道怎么用代码得到这个1,因为这不是标准的网页形式,所以每次代码得到的返回值都是空,请问一下,有没有哪个大哥知道这个问题咋解决,请帮一把,在这先谢谢了!
<span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Arial,Microsoft Yahei,Simsun,sans-serif'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; ">1</span>因为这个没有head和body,所以我不知道怎么用代码得到这个1,因为这不是标准的网页形式,所以每次代码得到的返回值都是空,请问一下,有没有哪个大哥知道这个问题咋解决,请帮一把,在这先谢谢了!
这个字符串是:
<span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Arial,Microsoft Yahei,Simsun,sans-serif'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; ">1</span>
我问别人呢,他们说是没有head和body,java程序解析不出来,不知道有什么方法可以把这个解析出来吗。
下面是我的代码:
HttpGet httpRequest = new HttpGet(szurl);
httpRequest.setHeader("Content-Type", "text/plain; charset=gb2312");
httpRequest.setHeader("Expect", "100-continue");
try
{
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 3000);
HttpConnectionParams.setSoTimeout(httpParameters, 5000);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
/*发送HTTP request*/
HttpResponse httpResponse = httpClient.execute(httpRequest);
/*若状态码为200 ok*/
if(httpResponse.getStatusLine().getStatusCode() == 200)
{
/*取得响应字符串*/
szrtn = EntityUtils.toString(httpResponse.getEntity());
}
else
{ }
}
catch (Exception e)
{
e.printStackTrace();
}
也就是说,请问各位有没有办法让代码得到不是正规html格式的返回值?
你好,我的代码get www.baidu.com的时候返回值都是对的,就是在请求代码中的网址的时候得到的返回值是空(在浏览器中打开代码中请求地址的话,返回值是有的,显示的是1),我不知道这是为什么,你可以帮我解答一下吗,谢谢了
以下是我的代码: String szrtn = "2";
String szurl = "http://61.155.150.200:8080/mulberry/start?buddle.cuiou=mulberry&catalog.cuiou=phone.regist&name=sgjv&password=q&phone=4563";
/*建立HTTP Get联机*/
HttpGet httpRequest = new HttpGet(szurl);
httpRequest.setHeader("Content-Type", "text/plain; charset=gb2312");
httpRequest.setHeader("Expect", "100-continue");
try
{
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 3000);
HttpConnectionParams.setSoTimeout(httpParameters, 5000);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
/*发送HTTP request*/
HttpResponse httpResponse = httpClient.execute(httpRequest);
/*若状态码为200 ok*/
if(httpResponse.getStatusLine().getStatusCode() == 200)
{
/*取得响应字符串*/
szrtn = EntityUtils.toString(httpResponse.getEntity());
}
else
{ }
}
catch (Exception e)
{
e.printStackTrace();
}
return szrtn;
主要原因是我没有设置
User-agent、Accept、Accept-Language和Accept-Charse的值。加上这几句httpRequest.setHeader("Content-Type", "text/plain; charset=gb2312");
httpRequest.setHeader("User-agent", "Mozilla/5.0 (Linux; U; Android 2.2; zh-cn; Desire_A8181 Build/FRF91) AppleWebKit/533.1 (KHTML, likeGecko) Version/4.0 Mobile Safari/533.1");
httpRequest.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
httpRequest.setHeader("Accept-Language", "zh-cn,en-us;q=0.7,en;q=0.3");
httpRequest.setHeader("Accept-Charse", "Big5,utf-8;q=0.7,*;q=0.7"); 问题就解决了