Android:下载HTML并不总是有效

在我的应用程序中,我使用以下代码下载网站的HTML样式表:

private DefaultHttpClient createHttpClient() { HttpParams my_httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(my_httpParams, 3000); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); ThreadSafeClientConnManager multiThreadedConnectionManager = new ThreadSafeClientConnManager(my_httpParams, registry); DefaultHttpClient httpclient = new DefaultHttpClient(multiThreadedConnectionManager, my_httpParams); return httpclient; } private class Example extends AsyncTask { int mStatusCode = 0; String content = ""; @Override protected Void doInBackground(Void... args) { String url = "www.example.com"; DefaultHttpClient httpclient = createHttpClient(); HttpGet httpget = new HttpGet(url); try { HttpResponse response = httpclient.execute(httpget); StatusLine statusLine = response.getStatusLine(); mStatusCode = statusLine.getStatusCode(); if (mStatusCode == 200){ content = EntityUtils.toString(response.getEntity()); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Void arg) { //Stuff } } 

但是,有时候,特别是当手机上3g时,我的mStatusCode = 0,而其他互联网应用程序,如浏览器仍然有效。

你们知道我怎么能防止这种情况发生吗?

很多人提前感谢!!

对于解析html,您可以使用jsoup – Java HTML Parser。 例如:

 String url = "http://www.google.com"; Document doc = Jsoup.connect(url).get(); Elements img = doc.select("img"); Elements js = doc.select("script"); // Save images for (Element el : img) { String imageUrl = el.attr("src"); FileUtils.saveFile("url", "File", "Folder"); } // Save JS for (int j = 0; j < js.size() - 1; j++) { String jsUrl = js.get(j).attr("src"); FileUtils.saveFile("url", "File", "Folder"); } // The same for CSS 

用于保存文件:

 public static void saveFile(String fileUrl, String fileName, String folderName) throws IOException { URL url = new URL(fileUrl); InputStream input = url.openStream(); File extStorageDirectory = Environment.getExternalStorageDirectory(); File folder = new File(extStorageDirectory, folderName); folder.mkdir(); OutputStream output = new FileOutputStream(new File(folder, fileName)); byte[] buffer = new byte[1024]; int bytesRead = 0; while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) { output.write(buffer, 0, bytesRead); } output.close(); input.close(); } 

要检查网络可用性,您可以使用此方法:

 public static boolean checkIfURLExists(String host, int seconds) { HttpURLConnection httpUrlConn; try { httpUrlConn = (HttpURLConnection) new URL(host).openConnection(); // Set timeouts in milliseconds httpUrlConn.setConnectTimeout(seconds * 1000); httpUrlConn.setReadTimeout(seconds * 1000); // Print HTTP status code/message for your information. System.out.println("Response Code: " + httpUrlConn.getResponseCode()); System.out.println("Response Message: " + httpUrlConn.getResponseMessage()); return (httpUrlConn.getResponseCode() == HttpURLConnection.HTTP_OK); } catch (Exception e) { System.out.println("Error: " + e.getMessage()); return false; } }