您的位置:首页 > 理论基础 > 计算机网络

HttpURLConnection--HttpURLConnection的基本用法

2015-11-11 13:26 537 查看
根据URL获取图片

private Bitmap getImageFromNet(String imageUrl) {
HttpURLConnection conn=null;
try {
//创建一个URL
URL url=new URL(imageUrl);
//获取一个连接
conn = (HttpURLConnection) url.openConnection();
//设置请求方式
conn.setRequestMethod("GET");
//设置连接超时
conn.setConnectTimeout(10000);
//设置读取超时
conn.setReadTimeout(10000);
//开始连接
conn.connect();
//获取连接响应码
int code = conn.getResponseCode();
if(code==200){
InputStream is=conn.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(is);
return bitmap;
}else{
Log.i("响应码", String.valueOf(code));
}

} catch (Exception e) {
e.printStackTrace();
}finally{
if(conn!=null){
conn.disconnect();
}
}
return null;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: