安卓异步处理 打开html代码
来源:互联网 发布:淘宝海外集运怎么收费 编辑:程序博客网 时间:2024/06/08 07:12
java代码
package com.example.ccc;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
public class MainActivity extends Activity {
private Button texte;
private TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
texte = (Button)findViewById(R.id.texte);
text = (TextView)findViewById(R.id.text);
texte.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
ReadURL("http://www.baidu.com");
}
});
}
public void ReadURL (String url) {
new AsyncTask<String, Void, String>(){
@Override
protected String doInBackground(String... arg0) {
try {
URL url =new URL(arg0[0]);
try {
URLConnection conn = url.openConnection();
InputStream is =conn.getInputStream();
InputStreamReader isr= new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
StringBuilder buli= new StringBuilder();
while ((line=br.readLine()) !=null) {
buli.append(line);
}
br.close();
isr.close();
is.close();
return buli.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
text.setText(result);
super.onPostExecute(result);
}
}.execute(url);
}
}
布局文件代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/texte"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="读取数据 "/>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/texte"
android:layout_below="@+id/texte" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="84dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</ScrollView>
</RelativeLayout>
别忘了添加权限
<uses-permission android:name="android.permission.INTERNET"/>
- 安卓异步处理 打开html代码
- 安卓异步处理的使用AsyncTask
- 安卓异步消息处理机制
- 安卓异步消息处理机制ALooperRoster
- 安卓异步消息处理机制ALooper
- 安卓异步任务
- 安卓异步消息处理机制源码流程
- 写给初学者30_android_安卓异步处理
- 安卓异步处理——AsyncTask<Params, Progress, Result>
- 【读书笔记】安卓异步任务
- 安卓异步任务AsyncTask
- 安卓异步—AsysncTask
- 安卓异步代理服务器客户端理解
- 安卓异步代理服务器客户端理解
- 安卓异步代理服务器客户端理解
- 安卓异步图片加载器
- 安卓开发之安卓异步加载AsyncTask
- ImageLoader 安卓异步加载图片开源工具类
- 解决/usr/bin/ld: cannot find -lxxx
- LeetCode 题解(45): Binary Tree Preorder Traversal
- 数据结构第二章 线性表 知识导图
- 深入hibernate的三种状态
- 组合数取模
- 安卓异步处理 打开html代码
- boost常用字符串处理方法学习
- 应届生校招找工作完整流程总结
- 毕 节 开 劳 务 费 发 票
- 长 沙 开 劳 务 费 发 票
- 福 州 开 劳 务 费 发 票
- What is Likelihood in terms of continuous probability distribution?
- magento 11个细节技巧
- Robot 第三周(16路PWM产生)