0
我想用某些關於某事的信息更新用戶。我想我會從網頁獲取一些信息並在TextView
中顯示。我已經寫了一些代碼,但它目前給這樣的輸出TextView
從網頁提取內容並在TextView中顯示
<html>
<head>
</head>
<body>
This is the test message for our user.
</body>
</html>
但我想,該消息應該只包含這一行,沒有別的。
This is the test message for our user.
我寫的代碼是:
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);
}
}
BINGO ..我得到了想要的輸出感謝 –
考慮接受的答案,如果它解決了你的問題:) – juned