Android 从网页获取内容并在文本视图中显示
我想向用户更新一些关于某事的信息。我想我会从网页上获取一些信息,并在Android 从网页获取内容并在文本视图中显示,android,html,inputstream,Android,Html,Inputstream,我想向用户更新一些关于某事的信息。我想我会从网页上获取一些信息,并在TextView中显示出来。我已经写了一些代码,但它目前在TextView <html> <head> </head> <body> This is the test message for our user. </body> </html> 使用 String htmlString=“这是针对我们用户的测试消息。”; setText(Html.fromH
TextView
中显示出来。我已经写了一些代码,但它目前在TextView
<html>
<head>
</head>
<body>
This is the test message for our user.
</body>
</html>
使用
String htmlString=“这是针对我们用户的测试消息。”;
setText(Html.fromHtml(htmlString));
public class MainActivity extends Activity {
private Button btnSkipContinue;
private TextView txtMessage;
private StringBuilder response;
private String text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
btnSkipContinue = (Button) findViewById(R.id.btnSkipCon);
txtMessage = (TextView) findViewById(R.id.txtMsgForUsers);
btnSkipContinue.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
finish();
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
try {
URLConnection connection = new URL("http://mywebiste.com/msg/MsgForUsers.html").openConnection();
connection.setRequestProperty("Accept-Charset", "UTF-8");
InputStream responseStream = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(responseStream));
response = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
response.append(line);
}
text = response.toString();
Log.i("Output", text);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
txtMessage.setText(text);
}
}
String htmlString = "<html><head></head><body>This is the test message for our user.</body></html>";
textView.setText(Html.fromHtml(htmlString));