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