当前位置: 代码迷 >> Android >> Android 请求网络图片的有关问题
  详细解决方案

Android 请求网络图片的有关问题

热度:81   发布时间:2016-04-28 07:22:43.0
Android 请求网络图片的问题
本帖最后由 xl_0715 于 2013-11-04 18:09:38 编辑
代码:

public static Bitmap getImage(String address) throws Exception {

        Bitmap imgmap = null;
InputStream is = null;

// 模拟浏览器访问图片
// 创建URL实例
URL url = new URL(address);

// 创建Http连接
HttpURLConnection  conn = (HttpURLConnection)url.openConnection();
try {
// 指定请求的方法(get、post)
conn.setRequestMethod("GET");
// 设置过期时间
conn.setConnectTimeout(5000);
// 获取服务器访问的流
is = conn.getInputStream();
byte[] imgbytes = StreamTool.getBytes(is);
imgmap = BitmapFactory.decodeByteArray(imgbytes, 0, imgbytes.length);

} finally {
if (is != null) {
is.close();
}
if (conn != null) {
conn.disconnect();
}
}

return imgmap;
}

代码执行没效果,我开的tomcat,在手机浏览器里可以正常访问到图片,
但是用上面的代码,调试的时候,报异常 java.net.ProtocolException: Connection already established
希望帮忙看看
android

------解决方案--------------------
HttpURLConnection??conn?=?(HttpURLConnection)url.openConnection();
这个conn.connect()一下要加。
  相关解决方案