3

I am right now working on an app which works as a book-stack,where user can read books of their choice,now what i am doing is,displaying the html pages that i've made,in a web-view in my application. Now this application works only if the user has full time internet connection on his phone. What exactly i want is when they first open the application,they would need internet connection and then the app should be able to download that page and store it in local database so the user can read it later on without having any internet connect. So is there any possible way to download the html page and store it in local database so user can use the app even if he is not connected to internet? I can post my code here if needed be. Any smallest tip or help would be really great as i am stuck here since long now:(

EDIT 1:

So i successfully downloaded the HTLM page from the website,but now the problem that i am facing is,that i cannot see any of the images of the downloaded html. What can be a proper solution for this?

1
  • Use file to save the downloaded content. Commented Jul 5, 2012 at 6:05

2 Answers 2

3

Here what's the mean of "Local Database"?

Preferred way is download your pages in either Internal Storage(/<data/data/<application_package_name>) (by default on non rooted device is private to your application) or on External Storage(public access). Then refer the pages from that storage area when user device has not a internet connection (offline mode).

Update: 1

To store those pages, you can use simple File read/write operation in Android.

For example:

String FILENAME = "hello_file";
String string = "hello world!";

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();

This example store file hello_file in your application's internal storage directory.

Update: 2 Download Web-Content

HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("http://www.xxxx.com");
HttpResponse response = httpClient.execute(httpGet, localContext);
String result = "";

BufferedReader reader = new BufferedReader(
    new InputStreamReader(
      response.getEntity().getContent()
    )
  );

String line = null;
while ((line = reader.readLine()) != null){
  result += line + "\n";
}

// Now you have the whole HTML loaded on the result variable

So write result variable in File, using my update 1 code. Simple.. :-)

Don't forget to add these two permission in your android application's manifest file.

  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  <uses-permission android:name="android.permission.INTERNET"></uses-permission>
Sign up to request clarification or add additional context in comments.

4 Comments

Yup i would exactly like to do that,but how to i download a single web-page? Like i want to download www.mytestpage.com/story1.htm. Can u please post a sample code? I am still beginner in this field and any help would be really appreciated. :)
ok so i downloaded this page successfully,but now the problem that i face is I cannot see the images in the offline mode,is there any solution for this problem? Cause images are the most important part of my application.
@ark same m facing problem, Have you got any solution related this issue?
How can I do this using volley ?
1

Code snippet for downloading web page. Check the comments in the code. Just provide the link ie www.mytestpage.com/story1.htm as downloadlink to the function

    void Download(String downloadlink,int choice)
{
    try {
        String USERAGENT;
        if(choice==0)
        {
            USERAGENT ="Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_7; en-us) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Safari/530.17";
        }
        else
        {
            USERAGENT ="Mozilla/5.0 (Linux; U; Android 2.1-update1; en-us; ADR6300 Build/ERE27) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17";
        }
        URL url = new URL(downloadlink);
        //create the new connection
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        //set up some things on the connection
        urlConnection.setRequestProperty("User-Agent", USERAGENT);  //if you are not sure of user agent just set choice=0
        urlConnection.setRequestMethod("GET");
        urlConnection.setDoOutput(true);
        urlConnection.connect();

        //set the path where we want to save the file
        File SDCardRoot = Environment.getExternalStorageDirectory();
        File dir = new File (SDCardRoot.getAbsolutePath() + "/yourfolder");
        if(!dir.exists())
        {
        dir.mkdirs();
        }
        File file = new File(dir, "filename");  //any name abc.html

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

        //write the contents to the file
        while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
            fileOutput.write(buffer, 0, bufferLength);
        }
        //close the output stream when done
        fileOutput.close();
        inputStream.close();
        urlConnection.disconnect();

    //catch some possible errors...
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

2 Comments

ok so i downloaded this page successfully,but now the problem that i face is I cannot see the images in the offline mode,is there any solution for this problem? Cause images are the most important part of my application.
The problem is that whenever you attempt to download from an url you download the html page and not the images. In order to accomplish this task you will either have to search for some library which does this or my solution would involve using htmlparser. First parse for <img tags fetch the location of image and call the function (which I have written in my answer) and save the image. Replace the img location attribute to the new location on the android device. Save the modified html page. For parsing I have used htmlcleaner in some of my apps.Unfortunately,I don't have a working code for this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.