界面效果如下,在编辑框中输入网址,点击按钮后,获取编辑框中的网址,打开HttpURLConnection连接,并获取输入流,将返回的流保存为html文件,然后再用WebView将html文件显示出来。
主要代码GetHtml.java(完整代码GetHtml.zip下载在下面的地址)
下载在Linux公社的1号FTP服务器里,下载地址:
FTP地址:ftp://www.linuxidc.com
用户名:
密码:
在 2011年LinuxIDC.com\8月\在Android上用HttpURLConnection获取网页内容
package ckl.gethtml; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.webkit.WebView; import android.widget.Button; import android.widget.EditText; public class GetHtml extends Activity { private EditText mEdit = null; private Button mButton = null; private WebView mWeb = null; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mEdit = (EditText)findViewById(R.id.myEdit1); mButton = (Button)findViewById(R.id.myButton1); mWeb = (WebView)findViewById(R.id.myWeb1); mWeb.getSettings().setJavaScriptEnabled(true); mWeb.getSettings().setPluginsEnabled(true); mButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { String strUrl = mEdit.getText().toString(); String strFile = "/sdcard/test.html"; if (!strUrl.startsWith("http://")) { strUrl = "http://" + strUrl; } getStaticPageByBytes(strUrl, strFile); mWeb.loadUrl("file://" + strFile); } }); } private void getStaticPageByBytes(String surl, String strFile){ Log.i("getStaticPageByBytes", surl + ", " + strFile); HttpURLConnection connection = null; InputStream is = null; File file = new File(strFile); FileOutputStream fos = null; try { URL url = new URL(surl); connection = (HttpURLConnection)url.openConnection(); int code = connection.getResponseCode(); if (HttpURLConnection.HTTP_OK == code) { connection.connect(); is = connection.getInputStream(); fos = new FileOutputStream(file); int i; while((i = is.read()) != -1){ fos.write(i); } is.close(); fos.close(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (connection != null) { connection.disconnect(); } } } }