HTML inside webView

I send some data to the server using the class DefaultHttpClient, and in the response stream I get an HTML file. I save the stream as a string and pass it to another action that contains a WebView to display 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 WebView download code 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, WebView cannot display HTML content correctly. Actually the screen displays an HTML string in URL encoded format. Interestingly, if I copy the output of Loggers into an HTML file and load this HTML code into my WebView (using webview.loadurl(file:///assets/xyz.html)), everything works fine.

I suspect some kind of character encoding problem.

What's going on here? Please, help.

Thank.

+3
1

BasicResponseHandler , . . . , StringBuffer.

, loadDataWithBaseURL(), loadData(). .

+1

Source: https://habr.com/ru/post/1738673/


All Articles