解析数据并在webview中加载html(Parse data and load html in a webview)
我有一个URL,我使用Jsoup来获取数据并删除标题。 但是当我加载到Webview时,一些图像没有显示,这就是我的问题。
我的代码在这里!
@Override protected Void doInBackground(Void... params) { try { Document doc = Jsoup.connect(URL).timeout(10 * 300) .userAgent(USER_AGENT_MOBILE).get(); doc.outputSettings().escapeMode(EscapeMode.xhtml); doc.select("div.header").remove(); String b = doc.toString(); webView.loadData(b, "text/html", "utf-8"); } catch (IOException e) { e.printStackTrace(); } return null; } }产量
I have a URL, I used Jsoup to get data and remove header. But when I load to Webview some images not show, that is my problem.
My code here !
@Override protected Void doInBackground(Void... params) { try { Document doc = Jsoup.connect(URL).timeout(10 * 300) .userAgent(USER_AGENT_MOBILE).get(); doc.outputSettings().escapeMode(EscapeMode.xhtml); doc.select("div.header").remove(); String b = doc.toString(); webView.loadData(b, "text/html", "utf-8"); } catch (IOException e) { e.printStackTrace(); } return null; } }Output
最满意答案
用这个
byte[] imageRaw = null; try { URL url = new URL("http://some.domain.tld/somePicture.jpg"); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); InputStream in = new BufferedInputStream(urlConnection.getInputStream()); ByteArrayOutputStream out = new ByteArrayOutputStream(); int c; while ((c = in.read()) != -1) { out.write(c); } out.flush(); imageRaw = out.toByteArray(); urlConnection.disconnect(); in.close(); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } String image64 = Base64.encodeToString(imageRaw, Base64.DEFAULT); String urlStr = "http://example.com/my.jpg"; String mimeType = "text/html"; String encoding = null; String pageData = "<img src=\"data:image/jpeg;base64," + image64 + "\" />"; WebView wv; wv = (WebView) findViewById(R.id.webview); wv.loadDataWithBaseURL(urlStr, pageData, mimeType, encoding, urlStr);use this
byte[] imageRaw = null; try { URL url = new URL("http://some.domain.tld/somePicture.jpg"); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); InputStream in = new BufferedInputStream(urlConnection.getInputStream()); ByteArrayOutputStream out = new ByteArrayOutputStream(); int c; while ((c = in.read()) != -1) { out.write(c); } out.flush(); imageRaw = out.toByteArray(); urlConnection.disconnect(); in.close(); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } String image64 = Base64.encodeToString(imageRaw, Base64.DEFAULT); String urlStr = "http://example.com/my.jpg"; String mimeType = "text/html"; String encoding = null; String pageData = "<img src=\"data:image/jpeg;base64," + image64 + "\" />"; WebView wv; wv = (WebView) findViewById(R.id.webview); wv.loadDataWithBaseURL(urlStr, pageData, mimeType, encoding, urlStr);更多推荐
发布评论