Android: Downloading HTML not always working

天涯浪子 提交于 2020-02-03 02:26:07

问题


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

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?

Many many thanks in advance!!


回答1:


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

For saving file:

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


来源:https://stackoverflow.com/questions/13550161/android-downloading-html-not-always-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!