Android 当我点击按钮时,我想下载整个网页

Android 当我点击按钮时,我想下载整个网页,android,file,url,save,Android,File,Url,Save,这是我的代码,它只能保存html,但当我点击按钮时,我的应用程序崩溃了。。!我的问题是 当我点击按钮时,我的应用程序崩溃了。为什么? 是否有任何方式,我可以保存完整的网页脱机查看plz帮助 public void onClick(View view) { download(); } }); } public void download() { try { //

这是我的代码,它只能保存html,但当我点击按钮时,我的应用程序崩溃了。。!我的问题是

当我点击按钮时,我的应用程序崩溃了。为什么? 是否有任何方式,我可以保存完整的网页脱机查看plz帮助

        public void onClick(View view) {
                    download();         
    }
});
        }
        public void download()
        {
    try {
//set the download URL, a url that points to a file on the internet
//this is the file to be downloaded
URL url = new URL("http://somewhere.com");
//create the new connection
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//set up some things on the connection
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
    urlConnection.connect();`

//set the path where we want to save the file
//in this case, going to save it on the root directory of the
//sd card.
File SDCardRoot = new File(Environment.getDataDirectory().getPath());
//create a new file, specifying the path, and the filename
//which we want to save the file as.
File file = new File(SDCardRoot,"somefile.txt");

//this will be used to write the downloaded data into the file we created
FileOutputStream fileOutput = new FileOutputStream(file);

//this will be used in reading the data from the internet
InputStream inputStream = urlConnection.getInputStream();

//this is the total size of the file
int totalSize = urlConnection.getContentLength();
//variable to store total downloaded bytes
int downloadedSize = 0;
//create a buffer...
byte[] buffer = new byte[1024];
int bufferLength = 0; //used to store a temporary size of the buffer*emphasized text*
//now, read through the input buffer and write the contents to the file
while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
    //add the data in the buffer to the file in the file output stream (the file on the sd card
    fileOutput.write(buffer, 0, bufferLength);
    //add up the size so we know how much is downloaded
    downloadedSize += bufferLength;
    //this is where you would do something to report the prgress, like this maybe
    //updateProgress(downloadedSize, totalSize);

}
//close the output stream when done
fileOutput.close();
//捕捉一些可能的错误。。。 }捕捉畸形{ e、 打印跟踪; }捕捉异常{ e、 打印跟踪; }

日志类别:


您正在主ui线程上执行网络请求,这是不允许的。使用异步任务

至于为什么Android会阻止这一点:你正试图通过点击按钮从互联网下载一些html内容。但是,如果在主线程上执行整个过程,ui将变得无响应,例如,在网络请求完成之前,按钮将无法工作

这有一个很好的示例代码来使用它:

编辑1:

关于如何下载html页面的源代码:

HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url); //Replace url with your own url. 
HttpResponse response = client.execute(request);

String html = "";
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null) {
    str.append(line);
}
in.close();
html = str.toString();

谢谢你知道如何下载正在保存的网站的源文件吗??就像chrome一样。。!选中编辑。只需在asynctask的doInBackground方法中使用此代码。对不起,没有人。我们可以帮助您解决特定问题,但我们无法为您编写完整的代码。重复的
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url); //Replace url with your own url. 
HttpResponse response = client.execute(request);

String html = "";
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null) {
    str.append(line);
}
in.close();
html = str.toString();