Android:下载 HTML 并不总是有效 [英] Android: Downloading HTML not always working

查看:27
本文介绍了Android:下载 HTML 并不总是有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my app, I download the HTML Stylesheet of a website, using this code:

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<Void, Void, Void> {

    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,而其他互联网应用程序(例如浏览器)仍在运行.

However, sometimes, especially when the phone is on 3g, I'm getting mStatusCode = 0, while other internet apps such a the browser still work.

你们知道我如何防止这种情况发生吗?

Do you guys know how I could prevent this from happening?

非常感谢!

推荐答案

对于解析 html,您可以使用 jsoup - JavaHTML 解析器.例如:

For parsing html you can use jsoup - Java HTML Parser. For example:

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();
}

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

For checking network availability you can use this method:

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;
    }
}

这篇关于Android:下载 HTML 并不总是有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆