Http网络通信--网络图片查看
2013-10-30 11:24
330 查看
1.要在andorid中实现网络图片查看,涉及到用户隐私问题,所以要在AndroidManifest.xml中添加访问网络权限
2.布局文件
3.MainActivity.java
在4.0以上版本的模拟器上运行以上代码,会抛出如下错误
10-30 02:05:28.418: E/AndroidRuntime(577): Caused by: android.os.NetworkOnMainThreadException
在这,引入一个anr的概念:
Anr :application not response 应用程序无响应
导致anr的原因:主线程需要做好多的事情,如:响应点击事件,更新UI
所以如果在主线程里面阻塞时间过长,应用程序就无响应
解决办法:为了避免出现anr,把所有耗时的操作放在子线程里面执行
出现以上的原因是4.0以上的模拟器不允许网络的操作在主线程里。而2.3版本的就没有这样的设置。
所以为了上程序无论在什么版本下都可以运行,做法就是把访问网络图片放进子线程里面执行
![](https://oscdn.geek-share.com/Uploads/Images/Content/202008/25/7cc12e2cc91827d3efc7736788f0cfbe)
修改MainActivity.java
效果
<uses-permission android:name="android.permission.INTERNET"/>
2.布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ImageView android:layout_weight="200" android:id="@+id/image" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <EditText android:id="@+id/path" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="请输入浏览地址" android:text="http://10.162.0.171:8080/Image/iamge.jpg" /> <Button android:id="@+id/button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="浏览图片" android:onClick="onClick" /> </LinearLayout>
3.MainActivity.java
package com.example.showimage; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import android.os.Bundle; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.text.TextUtils; import android.view.Menu; import android.view.View; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity { private ImageView image; private EditText path; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); image = (ImageView) findViewById(R.id.image); path = (EditText) findViewById(R.id.path); } public void onClick(View view) throws IOException{ String imagePath = path.getText().toString(); if(TextUtils.isEmpty(imagePath)){ Toast.makeText(MainActivity.this, "图片路径不能为空", Toast.LENGTH_LONG).show(); }else{ URL url = new URL(imagePath); //根据url发送http请求 HttpURLConnection conn=(HttpURLConnection) url.openConnection(); //设置请求方式 conn.setRequestMethod("GET"); //设置连接时间 conn.setConnectTimeout(5000); //响应编码 int code = conn.getResponseCode(); if(code==200){ //得到输入流 InputStream is=conn.getInputStream(); //位图 Bitmap bitmap=BitmapFactory.decodeStream(is); image.setImageBitmap(bitmap); }else{ Toast.makeText(MainActivity.this, "图片路径不能为空", Toast.LENGTH_LONG).show(); } } } }
在4.0以上版本的模拟器上运行以上代码,会抛出如下错误
10-30 02:05:28.418: E/AndroidRuntime(577): Caused by: android.os.NetworkOnMainThreadException
在这,引入一个anr的概念:
Anr :application not response 应用程序无响应
导致anr的原因:主线程需要做好多的事情,如:响应点击事件,更新UI
所以如果在主线程里面阻塞时间过长,应用程序就无响应
解决办法:为了避免出现anr,把所有耗时的操作放在子线程里面执行
出现以上的原因是4.0以上的模拟器不允许网络的操作在主线程里。而2.3版本的就没有这样的设置。
所以为了上程序无论在什么版本下都可以运行,做法就是把访问网络图片放进子线程里面执行
修改MainActivity.java
package com.example.showimage; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.text.TextUtils; import android.view.Menu; import android.view.View; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity { private ImageView image; private EditText path; private final int MESSAGE1=1; private final int MESSAGE2=2; //主线程创建消息处理器 private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { if(msg.what==MESSAGE1){ Bitmap bitmap =(Bitmap) msg.obj; image.setImageBitmap(bitmap);//这是修改ui }else if(msg.what==MESSAGE2){ Toast.makeText(MainActivity.this, "显示图片错误", Toast.LENGTH_LONG).show(); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); image = (ImageView) findViewById(R.id.image); path = (EditText) findViewById(R.id.path); } public void onClick(View view) throws IOException{ final String imagePath = path.getText().toString(); if(TextUtils.isEmpty(imagePath)){ Toast.makeText(MainActivity.this, "图片路径不能为空", Toast.LENGTH_LONG).show(); }else{ new Thread(){ @Override public void run() { try{ URL url = new URL(imagePath); //根据url发送http请求 HttpURLConnection conn=(HttpURLConnection) url.openConnection(); //设置请求方式 conn.setRequestMethod("GET"); //设置连接时间 conn.setConnectTimeout(5000); //响应编码 int code = conn.getResponseCode(); if(code==200){ //得到输入流 InputStream is=conn.getInputStream(); //位图 Bitmap bitmap=BitmapFactory.decodeStream(is); //告诉主线程,帮我修改ui Message msg = new Message(); msg.what=MESSAGE1; //handler处理的标志 msg.obj=bitmap; //将位图传给handler处理 handler.sendMessage(msg);//发送消息 //image.setImageBitmap(bitmap);//这是修改ui }else{ //告诉主线程,帮我修改ui Message msg = new Message(); msg.what=MESSAGE2; //handler处理的标志 handler.sendMessage(msg);//发送消息 //Toast在主线程显示,也需要放进子线程中 //Toast.makeText(MainActivity.this, "显示图片错误", Toast.LENGTH_LONG).show(); } }catch(Exception e){ e.printStackTrace(); Message msg = new Message(); msg.what=MESSAGE2; //handler处理的标志 handler.sendMessage(msg);//发送消息 } } }.start(); } } }
效果
相关文章推荐
- http status 汇总
- HTTP协议理解与应用总结
- apache http server 初始配置(静态页面访问)
- HTTP请求和响应头信息详解
- 网络直播电视m3u8
- 罗技高清网络摄像头 C270 64位win7适用软件
- Windows网络编程笔记3 ---- 邮槽和命名管道
- tcp/Ip http
- javascript-XMLHttpRequest
- Checksum: 0x7259 [incorrect, should be 0x0a75 (maybe caused by "TCP checksum offload"?)]
- 二维码的生成细节和原理 -- 转http://news.cnblogs.com/n/191671/
- http协议返回码
- 合理信息安全设备的选择 选型依据分析
- Linux网络编程wait()和waitpid()的讲解
- 认识信息安全设备 做好设备选型工作
- Linux网络编程使用多进程实现服务器并发访问
- Offline.js - 自动判断网络连接状态并提醒用户
- linux网络编程socket介绍
- 网络挂载
- http://ffmpeg-windows.googlecode.com/svn/trunk/