【发布时间】:2014-08-02 09:54:27
【问题描述】:
我是 android 开发的新手,我想将源 html 文本获取到 textview 或字符串。我还添加了许可互联网清单,但仍然无法将文本获取到 textview。请帮帮我。
我的代码是
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tv =(TextView)findViewById(R.id.textView1);
////////////////////////
HttpURLConnection connection = null;
InputStream is = null;
try{
connection = (HttpURLConnection) (new URL("http://sinhaladic.com/" )).openConnection();
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.connect();
// Read the response
StringBuffer buffer = new StringBuffer( );
is = connection.getInputStream();
BufferedReader br = new BufferedReader( new InputStreamReader( is ) );
String line = null;
while((line = br.readLine()) != null) {
tv.append(new String(line + "\r\n"));
}
is.close();
connection.disconnect();
} catch ( Exception e ){
e.printStackTrace();
} finally {
try {
is.close();
} catch (Throwable t) {
}
try {
connection.disconnect();
} catch (Throwable t) {
};
}
}//on create end
【问题讨论】:
-
您是否在 UI 线程上连接到网络?你的后台任务在哪里?
标签: android web-services http