HTTP的应用httpclient 和线程
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();
}
} }
}
HTTP的应用httpclient 和线程的更多相关文章
- httpclient模拟浏览器get\post
一般的情况下我们都是使用IE或者Navigator浏览器来访问一个WEB服务器,用来浏览页面查看信息或者提交一些数据等等.所访问的这些页面有的仅 仅是一些普通的页面,有的需要用户登录后方可使用,或者需 ...
- HttpClient 4.3教程(转载)
HttpClient 4.3教程(转载) 转自:http://www.yeetrack.com/?p=779 前言 Http协议应该是互联网中最重要的协议.持续增长的web服务.可联网的家用电器等都在 ...
- httpclient总结
1.httpclient总结:一.基本知识准备(1)构建URI工具类,URIBuilder(2)HttpResponse类,可以添加Header信息 获取所有Header信息的方法,调用HeaderI ...
- HttpClient学习记录-系列1(tutorial)
1. HttpClient使用了facade模式,如何使用的? 2. HTTP protocol interceptors使用了Decorator模式,如何使用的? URIBuilder respon ...
- httpclient的并发连接问题
昨天的搜索系统又出状况了,几个库同时重建索引变得死慢.经过一个上午的复现分析,确定问题出现httpclient的使用上(我使用的是3.1这个被广泛使用的遗留版本).搜索系统在重建索引时,是并发多个线程 ...
- 【工具】- HttpClient篇
简介 对于httpclient,相信很多人或多或少接触过,对于httpclient的使用姿势,相信很多人会有疑问?下面这边会通过代码说明 package xxx; import org.apache. ...
- 程序员节应该写博客之.NET下使用HTTP请求的正确姿势
程序员节应该写博客之.NET下使用HTTP请求的正确姿势 一.前言 去年9月份的时候我看到过外国朋友关于.NET Framework下HttpClient缺陷的分析后对HttpClient有了一定的了 ...
- 使用HttpClient4来构建Spring RestTemplate
Spring RestTemplate简单说明 现在REST服务已经很普及了,在我们的程序中,经常会需要调用REST API,这时候会有很多选择,原始一点的JDK自带的,再进一步点使用HttpClie ...
- httpclient4.5 的一些细节
本文转自:http://mercymessi.iteye.com/blog/2250161 httpclient是Apache下的一个用于执行http网络访问的一个工具包. 大致流程:新建一个http ...
随机推荐
- <转>九张图九句话
做个像石灰一样的人,别人越泼你冷水,你的人生越沸腾! 来自为知笔记(Wiz)
- php轮流排序,每隔一定的时间轮流进行位置排序,轮询的排行榜:function dataPollingInterval()
/* * @名称: php ,对数组每隔一定的时间(自设定时间)来轮流进行位置排序,轮询的排行榜. 精确到指定的秒 或 分钟 或 小时 或 天 ,对数据列表进行轮排. * @参数: (array)$l ...
- hadoop删除节点。
hadoop节点摘除操作: 1.确定exclude文件的位置. <property> <name>dfs.hosts.exclude</name> <valu ...
- 解决 xx.h has been modified since the precompiled header 系统头文件被修改
fatal error: file '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Dev ...
- JS URL参数传递 谷歌乱码解决
//第一个页面 var name=encodeURIComponent("参数"); var url="test1.html?name="+name; //第二 ...
- Windows 下的 Sublime Text 2 配置汇总, Sublime Text 3 亦可借鉴
1)软件下载地址:http://www.sublimetext.com/2 2)安装 Package Control ,方便安装和管理插件,网络资源很多,这里附上一篇:http://www.imjef ...
- 使用POI设置excel背景色
HSSFCellStyle setBorder1 = workbook.createCellStyle(); HSSFFont font1 = workbook.createFont(); font1 ...
- WPF 中保存 window(窗口)或者canvas成图片
最近需要用到这个功能,搜了一下不少代码有问题 ,找到一个效果比较好的,支持多级子元素 记一下. private void button_save_window_Click(object sender, ...
- spring技术核心概念纪要
一.背景 springframework 从最初的2.5版本发展至今,期间已经发生了非常多的修正及优化.许多新特性及模块的出现,使得整个框架体系显得越趋庞大,同时也带来了学习及理解上的困难. 本文阐述 ...
- UML的类图、时序图表示方法以及惯例
<UML类图图示样例>说明:http://wenku.baidu.com/link?url=DEaRFyVIAH4kZ7TolplfFhFhmAk3gFaNSOH7XPzfTnCWY4CB ...