使用http协议获取网络图片
来源:互联网 发布:电脑游戏用什么软件 编辑:程序博客网 时间:2024/06/05 22:32
http用于传输WWW方式的数据。http协议采用了请求响应的模型。在android中提供了HttpURLConnection和HttpClient接口开发HTTP程序。下面分别使用这两种方式获取网络图片。
1.HttpURLConnection
代码如下:
- public class HttpURLConnectionActivity extends Activity {
- private ImageView imageView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.simple1);
- imageView=(ImageView) this.findViewById(R.id.imageView1);
- //传入网络图片地址
- try {
- URL url = new URL("http://news.xinhuanet.com/photo/2012-02/09/122675973_51n.jpg");
- HttpURLConnection conn= (HttpURLConnection) url.openConnection();
- conn.setRequestMethod("GET");
- conn.setConnectTimeout(5*1000);
- conn.connect();
- InputStream in=conn.getInputStream();
- ByteArrayOutputStream bos=new ByteArrayOutputStream();
- byte[] buffer=new byte[1024];
- int len = 0;
- while((len=in.read(buffer))!=-1){
- bos.write(buffer,0,len);
- }
- byte[] dataImage=bos.toByteArray();
- bos.close();
- in.close();
- Bitmap bitmap=BitmapFactory.decodeByteArray(dataImage, 0, dataImage.length);
- //Drawable drawable=BitmapDrawable.
- imageView.setImageBitmap(bitmap);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- Toast.makeText(getApplicationContext(), "图片加载失败", 1).show();
- }
- }
- }
最后不要忘记在manifest.xml加入网络访问权限:
- <uses-permission android:name="android.permission.INTERNET" />
运行效果:
2.HttpClient
下面使用HttpClient获取网页内容:
- public class HttpClientActivity extends Activity {
- private ImageView imageview;
- private TextView text;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.simple2);
- imageview=(ImageView) this.findViewById(R.id.imageView2);
- text=(TextView) this.findViewById(R.id.textView2);
- HttpGet httpGet=new HttpGet("http://cloud.csdn.net/a/20120209/311628.html");
- HttpClient httpClient=new DefaultHttpClient();
- try {
- //得到HttpResponse对象
- HttpResponse httpResponse=httpClient.execute(httpGet);
- //HttpResponse的返回结果是不是成功
- if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
- //得到返回数据的字符串
- String dataImageStr=EntityUtils.toString(httpResponse.getEntity());
- text.setText(dataImageStr);
- }
- } catch (ClientProtocolException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
运行效果:
这样就成功加载了网页内容。
0 0
- 使用http协议获取网络图片
- 使用http协议获取网络图片
- 使用http协议获取网络图片
- 使用Http协议获取网络图片,采用servlet输出
- Android开发教程:使用http协议获取网络图片
- http编程系列2:http协议GET方式获取网络图片
- 使用HTTP协议访问网络
- 使用HTTP协议访问网络
- 使用Http协议请求网络
- 使用HTTP协议访问网络
- 使用HTTP协议访问网络
- 使用Http协议访问网络
- 使用http协议访问网络
- 使用HTTP协议访问网络
- 使用 HTTP协议访问网络
- android利用Http协议实现从网络上面获取图片的方法
- Android利用HTTP协议加载网络图片
- http协议GET方式获取图片
- Unity3D Shader学习(一)
- Redis中的跳跃表
- PHP Web ApplicationPHP Debug 配置方法
- namespace
- 返回码
- 使用http协议获取网络图片
- iOS开发:sqlite的简单实用
- c++ 使用stringstream对象简化类型转换
- PYTHON-进阶-编码处理小结
- Nagle算法
- 数据库设计Step by Step (10)——范式化
- Ratchet教程:meta与link标签
- CQOI2016题目&CQOI2016题解&不同的最小割&K远点对&手机号码&密匙破解&路由表&伪光滑数
- nginx-Brew安装lua模块