Webview 加载文章内容

设置listview的点击事件

ListView newslistView=(ListView) findViewById(R.id.show_news);
newslistView.setAdapter(new NewsAdapter(newss,NewsActivity.this,newslistView));
newslistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
               public void onItemClick(AdapterView<?> parent, View view, int   position, long id) {
                Intent intent = new Intent(NewsActivity.this,ArticleActivity.class);
                News news = newss.get(position);
                String AtUrl = "http://news-at.zhihu.com/api/4/news/"+news.getId();
                intent.putExtra("AtUrl",AtUrl);
                startActivity(intent);
        }
});

新建WebView,通过url获取html内容,问题是加载css的过程。
webview.load()这个方法会出现乱码,查过之后发现要手动设置编码方式为utf8

WebView wv = (WebView)findViewById(R.id.webview);
String content = getUnicodeContent();
wv.getSettings().setDefaultTextEncodingName(“UTF -8”);
wv.loadData(content, “text/html”, “UTF-8”) ;

但仍没有解决乱码,所以改用loadDataWithBaseURL()

     * @param baseUrl the URL to use as the page's base URL. If null defaults to
     *                'about:blank'.
     * @param data a String of data in the given encoding
     * @param mimeType the MIMEType of the data, e.g. 'text/html'. If null,
     *                 defaults to 'text/html'.
     * @param encoding the encoding of the data
     * @param historyUrl the URL to use as the history entry. If null defaults
     *                   to 'about:blank'. If non-null, this must be a valid URL.
     */
    public void loadDataWithBaseURL(String baseUrl, String data,
            String mimeType, String encoding, String historyUrl) {
        checkThread();
        mProvider.loadDataWithBaseURL(baseUrl, data, mimeType, encoding, historyUrl);
    }

加载css的方法貌似有很多,比如用js,但最后选择了在以下方案。
要注意的是获取到的html代码不一样的情况下面的代码是有问题的,所以这个方法是偷懒的权宜之计

WebView webview = (WebView) findViewById(R.id.ArticleView);
String html = "<html><header><style type='text/css'>"+css+"</style></header>"
                +"<body>"+body+"</body></html>";
webview.loadDataWithBaseURL(null,html, "text/html",  "utf-8", null);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,982评论 25 708
  • WebView·开车指南 2016-08-31BugDev 北京市东城区首席Bug布道师开山之作,一整月交通事故血...
    53c021c38a1d阅读 847评论 0 1
  • 转载请注明出处: Webview秒开框架VasSonic源码分析(一) Webview秒开框架VasSonic源码...
    陈文超happylion阅读 8,170评论 2 16
  • WebView·开车指南 目录 WebView简介 WebView基本使用 WebView常用方法 WebSett...
    小庄bb阅读 3,536评论 3 25
  • “世间很多最初定理都会变成终极原理。” 有意思有意思,道就是一,因缘和合。 细想,好像世间所有的学问也是如此,最终...
    清自欢阅读 953评论 0 1