Android大侠进来看一下,标题是不是要长长长长长长长长长长长长长长长长啊!!!

wudongfei1 2013-04-26 11:06:07
问题描述如下:
现在我想实现根据URL获取网页的html URL地址:http://nlp.xiaoi.com/robot/demo/wap/wap-demo.action?requestContent=why
我的实现如下

public class MainActivity extends Activity {
private Button getContent=null;
private HtmlUtil util=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getContent=(Button) findViewById(R.id.getContent);
getContent.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
util=new HtmlUtil();
String result=util.posturl("http://nlp.xiaoi.com/robot/demo/wap/wap-demo.action?requestContent=why");
System.out.println(result);

}
}).start();
}
});
}
}


public class HtmlUtil {


public String posturl(String url){
InputStream is = null;
String result = "";

try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
e.printStackTrace();
return "Fail to establish http connection!"+e.toString();
}

try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();

result=sb.toString();
}catch(Exception e){
e.printStackTrace();
return "Fail to convert net stream!";
}

return result;
}


public InputStream streampost(String remote_addr){
URL infoUrl = null;
InputStream inStream = null;
try {
infoUrl = new URL(remote_addr);
URLConnection connection = infoUrl.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection)connection;
httpConnection.setRequestMethod("GET");
int responseCode = httpConnection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK){
inStream = httpConnection.getInputStream();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return inStream;
}
//读取输入流中的数据,返回字节数组byte[]
public byte[] readInputStream(InputStream inStream) throws Exception{
//此类实现了一个输出流,其中的数据被写入一个 byte 数组
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
// 字节数组
byte[] buffer = new byte[1024];
int len = 0;
//从输入流中读取一定数量的字节,并将其存储在缓冲区数组buffer 中
while ((len = inStream.read(buffer)) != -1) {
// 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流
outStream.write(buffer, 0, len);
}
inStream.close();
//toByteArray()创建一个新分配的 byte 数组。
return outStream.toByteArray();
}
}


打印出来的信息如下

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Cache-Control" content="no-cache" />
<meta name="viewport" content="width=device-width;initial-scale=1.4;minimum-scale=1.0;maximum-scale=2.0" />
<meta name="MobileOptimized" content="240" />
<meta name="robots" content="index,nofollow" />
<link rel="stylesheet" type="text/css" href="css/iphone_wap_.css"/>
<title>小i智能机器人WAP展示</title>
</head>
<body>
<div class="wap_wrap" style="background : none; padding : 0; margin: 0;">
<div class="wap_content left">
<div class="wap_banner"><img src="images/banner1.jpg" width="222" height="40" title="小i机器人" alt="小i机器人"></div>
<div class="wap_huanying">您好,我是小i机器人,请直接输入您想咨询的问题,谢谢!</div>
<div class="wap_search">
<div class="wap_searchcn1">
<form id="askForm" action="wap-demo.action" method="post">
<input style="border : 1px solid #F48102;" id="askInput" name="requestContent" type="text" autofocus="autofocus"/>
<input name="" value="提问" type="submit" />
</form>
</div>
<ul class="wap_searchul left" style="padding:0;">
<p class="wap_searchulp1">快速提问:</p>
<li><a href="wap-demo.action?requestContent=什么是智能网络机器人?">什么是智能网络机器人?</a></li>
04-26 10:54:12.430: I/System.out(1470): <li><a href="wap-demo.action?requestContent=小i机器人可以做什么?">小i机器人可以做什么?</a></li>
</ul>
<div class="wap_content2 left">
<p class="wap_cn1"><b>我:</b>why</p>
<p class="wap_cn2"></p>
</div>
</div>
</div>
</div>
<script src="../../js/verify.js"></script>
</body>
</html>

但是在电脑上浏览器端的html却是:


<?xml version="1.0" encoding="utf-8"?>




<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Cache-Control" content="no-cache" />
<meta name="viewport" content="width=device-width;initial-scale=1.4;minimum-scale=1.0;maximum-scale=2.0" />
<meta name="MobileOptimized" content="240" />
<meta name="robots" content="index,nofollow" />
<link rel="stylesheet" type="text/css" href="css/iphone_wap_.css"/>
<title>小i智能机器人WAP展示</title>
</head>
<body>
<div class="wap_wrap" style="background : none; padding : 0; margin: 0;">
<div class="wap_content left">
<div class="wap_banner"><img src="images/banner1.jpg" width="222" height="40" title="小i机器人" alt="小i机器人"></div>
<div class="wap_huanying">您好,我是小i机器人,请直接输入您想咨询的问题,谢谢!</div>
<div class="wap_search">

<div class="wap_searchcn1">
<form id="askForm" action="wap-demo.action" method="post">
<input style="border : 1px solid #F48102;" id="askInput" name="requestContent" type="text" autofocus="autofocus"/>
<input name="" value="提问" type="submit" />
</form>
</div>

<ul class="wap_searchul left" style="padding:0;">
<p class="wap_searchulp1">快速提问:</p>
<li><a href="wap-demo.action?requestContent=什么是智能网络机器人?">什么是智能网络机器人?</a></li>
<li><a href="wap-demo.action?requestContent=小i机器人可以做什么?">小i机器人可以做什么?</a></li>
</ul>

<div class="wap_content2 left">
<p class="wap_cn1"><b>我:</b>why</p>

<p class="wap_cn2"><span>小i机器人:</span> don't you tell me?
</p>
</div>
</div>
</div>
</div>
<script src="../../js/verify.js"></script>
</body>
</html>

太诡异了
...全文
158 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
wudongfei1 2013-04-27
  • 打赏
  • 举报
回复
引用 9 楼 wenwei19861106 的回复:
有区别吗 就多了几个空行而已 正则去掉就行了
我要的是第40行的答案
wudongfei1 2013-04-27
  • 打赏
  • 举报
回复
引用 10 楼 xxjin 的回复:
[quote=引用 6 楼 wudongfei1 的回复:] 哎~还没人啊
找到问题所在了。 你第一次打开浏览器,直接在地址栏输入“http://nlp.xiaoi.com/robot/demo/wap/wap-demo.action?requestContent=why”,返回的结果应该也是“<p class="wap_cn1"><b>我:</b>why</p> <p class="wap_cn2"></p>”。再次刷新的时候,才会有结果。 说明在cookie上面做了手脚。你参照“<script src="../../js/verify.js"></script>”这个js文件里面修改cookie的方式,在你的代码里面修改cookie。应该就行了。[/quote] 果然是高手
小村长 2013-04-27
  • 打赏
  • 举报
回复
引用 10 楼 xxjin 的回复:
[quote=引用 6 楼 wudongfei1 的回复:] 哎~还没人啊
找到问题所在了。 你第一次打开浏览器,直接在地址栏输入“http://nlp.xiaoi.com/robot/demo/wap/wap-demo.action?requestContent=why”,返回的结果应该也是“<p class="wap_cn1"><b>我:</b>why</p> <p class="wap_cn2"></p>”。再次刷新的时候,才会有结果。 说明在cookie上面做了手脚。你参照“<script src="../../js/verify.js"></script>”这个js文件里面修改cookie的方式,在你的代码里面修改cookie。应该就行了。[/quote] 楼上的大侠真NB,话说大侠是怎么发现的?
水镜123 2013-04-27
  • 打赏
  • 举报
回复
引用 6 楼 wudongfei1 的回复:
哎~还没人啊
找到问题所在了。 你第一次打开浏览器,直接在地址栏输入“http://nlp.xiaoi.com/robot/demo/wap/wap-demo.action?requestContent=why”,返回的结果应该也是“<p class="wap_cn1"><b>我:</b>why</p> <p class="wap_cn2"></p>”。再次刷新的时候,才会有结果。 说明在cookie上面做了手脚。你参照“<script src="../../js/verify.js"></script>”这个js文件里面修改cookie的方式,在你的代码里面修改cookie。应该就行了。
南瓜饼 2013-04-27
  • 打赏
  • 举报
回复
有区别吗 就多了几个空行而已 正则去掉就行了
wudongfei1 2013-04-27
  • 打赏
  • 举报
回复
引用 7 楼 franzhong 的回复:
那个URL是你的吗,访问来源或方式不同造成了返回结果不同?
URL 不是我的
水镜123 2013-04-27
  • 打赏
  • 举报
回复
实践证明,
引用 12 楼 wudongfei1 的回复:
[quote=引用 10 楼 xxjin 的回复:] [quote=引用 6 楼 wudongfei1 的回复:] 哎~还没人啊
找到问题所在了。 你第一次打开浏览器,直接在地址栏输入“http://nlp.xiaoi.com/robot/demo/wap/wap-demo.action?requestContent=why”,返回的结果应该也是“<p class="wap_cn1"><b>我:</b>why</p> <p class="wap_cn2"></p>”。再次刷新的时候,才会有结果。 说明在cookie上面做了手脚。你参照“<script src="../../js/verify.js"></script>”这个js文件里面修改cookie的方式,在你的代码里面修改cookie。应该就行了。[/quote] 果然是高手[/quote] 实践证明,确实是这样的。
水镜123 2013-04-26
  • 打赏
  • 举报
回复
引用 2 楼 wudongfei1 的回复:
太现实了,看来只有女生的问题才会被回答
变个性?
小村长 2013-04-26
  • 打赏
  • 举报
回复
屌丝 这都不会。。。
wudongfei1 2013-04-26
  • 打赏
  • 举报
回复
太现实了,看来只有女生的问题才会被回答
franzhong 2013-04-26
  • 打赏
  • 举报
回复
那个URL是你的吗,访问来源或方式不同造成了返回结果不同?
wudongfei1 2013-04-26
  • 打赏
  • 举报
回复
哎~还没人啊
wudongfei1 2013-04-26
  • 打赏
  • 举报
回复
沙发自己坐,好人一生平安
wudongfei1 2013-04-26
  • 打赏
  • 举报
回复
引用 4 楼 xxjin 的回复:
[quote=引用 2 楼 wudongfei1 的回复:] 太现实了,看来只有女生的问题才会被回答
变个性?[/quote] 变性代价太大了

80,362

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧