80,362
社区成员
发帖
与我相关
我的任务
分享
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>
<?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>