How to save .HTML page/file in application's database in android?

后端 未结 2 691
说谎
说谎 2020-12-18 12:21

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

2条回答
  •  余生分开走
    2020-12-18 12:42

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

提交回复
热议问题