how to get html content from a webview?

后端 未结 12 1563
鱼传尺愫
鱼传尺愫 2020-11-22 04:29

Which is the simplest method to get html code from a webview? I have tried several methods from stackoverflow and google, but can\'t find an exact method. Please mention an

12条回答
  •  被撕碎了的回忆
    2020-11-22 05:15

    above given methods are for if you have an web url ,but if you have an local html then you can have also html by this code

    AssetManager mgr = mContext.getAssets();
                 try {
    InputStream in = null;              
    if(condition)//you have a local html saved in assets
                                {
                                in = mgr.open(mFileName,AssetManager.ACCESS_BUFFER);
                               }
                                else if(condition)//you have an url
                                {
                                URL feedURL = new URL(sURL);
                      in = feedURL.openConnection().getInputStream();}
    
                                // here you will get your html
                     String sHTML = streamToString(in);
                     in.close();
    
                     //display this html in the browser or web view              
    
    
                 } catch (IOException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
                 }
            public static String streamToString(InputStream in) throws IOException {
                if(in == null) {
                    return "";
                }
    
                Writer writer = new StringWriter();
                char[] buffer = new char[1024];
    
                try {
                    Reader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
    
                    int n;
                    while ((n = reader.read(buffer)) != -1) {
                        writer.write(buffer, 0, n);
                    }
    
                } finally {
    
                }
    
                return writer.toString();
            }
    

提交回复
热议问题