HTTP的应用httpclient 和线程
2024-09-30 05:21:12
package com.example.service; import java.io.IOException;
import java.io.InputStream; import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient; import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.widget.ImageView; public class MainActivity extends Activity { static ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image=(ImageView) findViewById(R.id.image);
/* Intent intent=new Intent(this, MyService.class);
startService(intent);*/ new thrad().start(); }
Handler handle=new Handler(){ @Override
public void handleMessage(Message msg) { image.setImageBitmap((Bitmap) msg.obj);
} };
class thrad extends Thread{ @Override
public void run() {
HttpClient http=new DefaultHttpClient();
HttpGet request=new HttpGet("http://dh2.kimg.cn/www/meinv/5/7f/547f54c2a9712b8b2e7be7a5ca2fe5fd-af35e4");
HttpResponse response = null;
try {
response = http.execute(request);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStream in = null;
try {
in = response.getEntity().getContent();
//String str=EntityUtils.toString(response.getEntity());
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//服务器返回的数据
Bitmap bitmap=BitmapFactory.decodeStream(in);
Message msg=handle.obtainMessage();
msg.obj=bitmap;
handle.sendMessage(msg);
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
}
最新文章
- bzoj1901
- 第二天ci项目规划 数据库设计
- ToolBar和DrawerLayout的使用实现侧拉栏抽屉的开闭
- Android -- Activity,Fragment lifecycle
- 【转载】Ubuntu下SVN安装和配置
- json学习系列(6)JSONObject和JSONArray是JDK的集合部分延伸
- (转)SqlServer将数据库中的表复制到另一个数据库
- CentOS安装中文输入法
- Java三大主流框架概述(转载)
- Tomcat,Weblogic,WebSphere,JBoss四种服务器简单对比
- box-size
- HDU 5677 ztr loves substring
- Android报错:WindowManager$BadTokenException: Unable to add window -- window has already been added
- java工作流引擎Jflow流程事件和流程节点事件设置
- 分布式缓存技术之Redis_03分布式redis
- [NOI2009]诗人小G(dp + 决策单调性优化)
- java之servlet学习基础(二)
- 总结 Linux 下安装 PHP 扩展步骤
- Confluence 6 数据库连接方式
- http-cookie、session、Token
热门文章
- PHP Html5上传大文件
- JavaScript作用域学习笔记
- [转]Oracle 树操作(select…start with…connect by…prior)
- 论文笔记之:Dueling Network Architectures for Deep Reinforcement Learning
- Vmvare下Ubuntu安装Python3.4
- Maven根据不同个环境打包, 获取不同的配置文件等等
- 怎么 才能显示Eclipse中Console的全部内容
- api接口签名验证(MD5)
- 使用-MM生成include指令和依赖生成(make include directive and dependency generation with -MM)
- [zz]Lessons from Pixar: Why Software Developers Should Be Storytellers