HTML inside webView

Posted by Samuh on Stack Overflow See other posts from Stack Overflow or by Samuh
Published on 2010-03-26T14:19:50Z Indexed on 2010/03/26 14:23 UTC
Read the original article Hit count: 627

I am posting some data to a server using DefaultHttpClient class and in the response stream I am getting a HTML file. I save the stream as a string and pass it onto another activity which contains a WebView to render this HTML on the screen:

response = httpClient.execute(get);
InputStream is = response.getEntity().getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is,"utf-8"));
StringBuffer sb = new StringBuffer();
String line;

while((line=br.readLine())!=null){
    sb.append(line);
    sb.append("\n");
}

is.close();

Intent intent = new Intent(this,Trial.class);
intent.putExtra("trial",sb.toString());
startActivity(intent);

Log.i("SB",sb.toString());

In Second Activity, the code to load the WebView reads:

WebView browser = ((WebView)findViewById(R.id.trial_web));
browser.getSettings().setJavaScriptEnabled(true); 
browser.loadData(html,"text/html", "utf-8");

When I run this code, the WebView is not able to render the HTML content properly. It actually shows the HTML string in URL encoded format on the screen. Interestingly, If I copy the Loggers output to HTML file and then load this HTML in my WebView(using webview.loadurl(file:///assets/xyz.html)) everything works fine.

I suspect some problem with character encoding.

What is going wrong here? Please help.

Thanks.

© Stack Overflow or respective owner

Related posts about android

Related posts about webview