用POST方式,HTTP/1.1 405 Method NOT Allowed. 于是用GET,得到响应。
// TODO Auto-generated method stub
//你可以把这个url复制到pc浏览器地址栏,就是搜索后的结果。
HttpGet httpRequest = new HttpGet(uriAPI);
try
{
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
if(httpResponse.getStatusLine().getStatusCode() == 200)
{
String strResult = EntityUtils.toString(httpResponse.getEntity());
mTextView1.setText("this is a get");
WebView wb;
wb=(WebView)findViewById(R.id.webview);
wb.getSettings().setJavaScriptEnabled(true);
wb.loadDataWithBaseURL("",strResult, "text/html", "UTF-8",""); //显示html,测试结果显示的是google首页。
Toast.makeText(getBaseContext(), uriAPI, Toast.LENGTH_LONG).show();
try{
File myFile = new File("/sdcard/google.html");
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
fOut.write(strResult.getBytes());
//在这里为了进一步搞清楚数据内容,我把他写到了文件中。文件在附件中,用浏览器打开确实是google首页。问题出在哪啊?
fOut.flush();
fOut.close();
}catch(Exception e){
e.printStackTrace(System.err);
Toast.makeText(getBaseContext(), "write error", Toast.LENGTH_LONG).show();
}
}
else
{
mTextView1.setText("Error Response: "+httpResponse.getStatusLine().toString());
}
}
catch (ClientProtocolException e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
}
catch (IOException e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
}
catch (Exception e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
}
}
悲催啊!!