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

Android大侠进来看一下,标题是不是要长长长长长长长长长长长长长长长长啊!!!本帖最后由 wudongfei1 于 201

Android大侠进来看一下,标题是不是要长长长长长长长长长长长长长长长长啊!!!
本帖最后由 wudongfei1 于 2013-04-26 11:50:35 编辑 问题描述如下:
现在我想实现根据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="Android大侠进入看一下,标题是不是要长长长长长长长长长长长长长长长长啊"></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="Android大侠进入看一下,标题是不是要长长长长长长长长长长长长长长长长啊"></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>


太诡异了 Android HTML
[解决办法]
屌丝 这都不会。。。
[解决办法]
那个URL是你的吗,访问来源或方式不同造成了返回结果不同?
[解决办法]
引用:
哎~还没人啊


找到问题所在了。
你第一次打开浏览器,直接在地址栏输入“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。应该就行了。