Android httpGet 使用 以及使用Handler异步更新textview的text值
2013-05-07 11:48
721 查看
在 Activity中定义3个属性
初始化属性值
源代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handler = new Handler();
text = (TextView) findViewById(R.id.text);
new Thread() {
public void run() {
String str = "http://www.hbecoop.com/index.html";
HttpResponse httpResponse = null;
HttpGet get = new HttpGet(str);
try {
httpResponse = (new DefaultHttpClient()).execute(get);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
try {
result = EntityUtils.toString(httpResponse.getEntity());
handler.post(new Runnable() {
@Override
public void run() {
text.setText(result);
}
});
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}.start();
}
}
备注:
需要注意几点
1:需要在xml中添加网络访问权限 <uses-permission android:name="android.permission.INTERNET" />
2:如果httpget直接在主线程里面使用有可能会抛出异常,导致程序崩溃 建议使用异步的形式来获取资源
private Handler handler; //实现异步更新textview值 private String result;//保存get返回的字符串 private TextView text;//textview控件
初始化属性值
//初始化的时候代码要在 onCreate方法中初始化 不能在子线程中
handler = new Handler();
text = (TextView) findViewById(R.id.text);
源代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handler = new Handler();
text = (TextView) findViewById(R.id.text);
new Thread() {
public void run() {
String str = "http://www.hbecoop.com/index.html";
HttpResponse httpResponse = null;
HttpGet get = new HttpGet(str);
try {
httpResponse = (new DefaultHttpClient()).execute(get);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
try {
result = EntityUtils.toString(httpResponse.getEntity());
handler.post(new Runnable() {
@Override
public void run() {
text.setText(result);
}
});
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}.start();
}
}
备注:
需要注意几点
1:需要在xml中添加网络访问权限 <uses-permission android:name="android.permission.INTERNET" />
2:如果httpget直接在主线程里面使用有可能会抛出异常,导致程序崩溃 建议使用异步的形式来获取资源
相关文章推荐
- 安卓中引入第三方架包butterKnife的操作方法以及使用Intent创建选择窗口和通过handler发消息实时更新textview中的时间
- Handler原理与使用,以及Android中异步更新的UI的方式
- Android异步机制一:使用Thread+Handler实现非UI线程更新UI界面
- Android异步更新UI的方式之使用Handler消息传递机制
- android中使用异步调用google翻译接口翻译语句,并用译文替换显示到textView的原文
- android客户端通过Get方式提交参数给服务器,使用URL和HttpURLConnection实现,以及乱码问题解决
- Android中使用一个textview实现不同颜色以及内容分割的显示
- Android异步更新UI的方式之使用Handler的post(Runnabel r)方法
- Androidx学习笔记(40)--- 使用异步HttpClient(android-async-http-master)做get提交
- Android使用自己封装的Http和Thread、Handler实现异步任务
- Android在TextView中显示html标签以及异步加载网页图片
- Android异步机制一:使用Thread+Handler实现非UI线程更新UI界面
- android - TextView px和sp转换,以及getTextSize和setTextSize单位
- (转)Android - TextView px和sp转换,以及getTextSize和setTextSize单位
- android客户端通过Get方式提交参数给服务器,使用URL和HttpURLConnection实现,以及乱码问题解决
- 第十二篇:Android GET请求获取网络数据以及POST数据(使用HttpURLConnection)
- Android 异步任务 设置 超时使用handler更新通知功能
- Android使用handler和Runnable结合,自定义View更新UI的Demo
- 解决TextView数据不能更新的问题以及Android Button事件响应函数的两种方法
- 11_Android中HttpClient的应用,读取网络xml及xml解析流,Handler的应用,LayoutInflater的使用,SmartImageView的使用