V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Registering
V2EX  ›  Android

像下面这样用 httpurlconnetion 下载,为什么报错。

  •  
  •   Registering · 2014-10-28 11:11:42 +08:00 · 8956 次点击
    这是一个创建于 3679 天前的主题,其中的信息可能已经有所发展或是发生改变。
    某张图片的下载地址为:“http://10.13.73.25:8080/family_image/download.dodownloadfile=mmexport1412173988852.jpg”

    private Bitmap getImageBitmap() {
    URL imgUrl = null;
    Bitmap bitmap = null;
    try {
    imgUrl = new URL("http://10.13.73.25:8080/family_image/download.do?downloadfile=mmexport1412173988852.jpg");
    HttpURLConnection conn = (HttpURLConnection) imgUrl
    .openConnection();
    conn.setReadTimeout(10000 /* milliseconds */);
    conn.setConnectTimeout(15000 /* milliseconds */);
    conn.setRequestMethod("GET");
    conn.setDoInput(true);
    conn.connect();

    InputStream is = conn.getInputStream();
    bitmap = BitmapFactory.decodeStream(is);
    is.close();
    } catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    return bitmap;
    }


    报错如下:
    10-28 11:01:57.650: W/System.err(6326): java.net.SocketException: socket failed: EACCES (Permission denied)
    10-28 11:01:57.650: W/System.err(6326): at libcore.io.IoBridge.socket(IoBridge.java:576)
    10-28 11:01:57.650: W/System.err(6326): at java.net.PlainSocketImpl.create(PlainSocketImpl.java:201)
    10-28 11:01:57.650: W/System.err(6326): at java.net.Socket.checkOpenAndCreate(Socket.java:664)
    10-28 11:01:57.650: W/System.err(6326): at java.net.Socket.connect(Socket.java:808)
    10-28 11:01:57.650: W/System.err(6326): at com.android.okhttp.internal.Platform.connectSocket(Platform.java:131)
    10-28 11:01:57.655: W/System.err(6326): at com.android.okhttp.Connection.connect(Connection.java:101)
    10-28 11:01:57.655: W/System.err(6326): at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:294)
    10-28 11:01:57.655: W/System.err(6326): at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
    10-28 11:01:57.655: W/System.err(6326): at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
    10-28 11:01:57.655: W/System.err(6326): at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
    10-28 11:01:57.655: W/System.err(6326): at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
    10-28 11:01:57.655: W/System.err(6326): at com.example.downloadtest.MainActivity.getImageBitmap(MainActivity.java:67)
    10-28 11:01:57.655: W/System.err(6326): at com.example.downloadtest.MainActivity.access$0(MainActivity.java:55)
    10-28 11:01:57.655: W/System.err(6326): at com.example.downloadtest.MainActivity$1$1.run(MainActivity.java:40)
    10-28 11:01:57.655: W/System.err(6326): at java.lang.Thread.run(Thread.java:841)
    10-28 11:01:57.655: W/System.err(6326): Caused by: libcore.io.ErrnoException: socket failed: EACCES (Permission denied)
    10-28 11:01:57.655: W/System.err(6326): at libcore.io.Posix.socket(Native Method)
    10-28 11:01:57.655: W/System.err(6326): at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:181)
    10-28 11:01:57.655: W/System.err(6326): at libcore.io.IoBridge.socket(IoBridge.java:561)
    10-28 11:01:57.655: W/System.err(6326): ... 14 more
    Registering
        1
    Registering  
    OP
       2014-10-28 11:14:58 +08:00
    下载地址为:
    http://10.13.73.25:8080/family_image/download.do?downloadfile=mmexport1412173988852.jpg
    StackGao
        2
    StackGao  
       2014-10-28 11:15:44 +08:00   ❤️ 1
    <uses-permission android:name="android.permission.INTERNET"/>
    Registering
        3
    Registering  
    OP
       2014-10-28 11:26:39 +08:00
    @StackGao 好吧,晚上鲁多了
    Registering
        4
    Registering  
    OP
       2014-10-28 11:27:39 +08:00
    @StackGao 一直提防这个问题,所以从没见过这个bug, 第一次出现了,反而不认识它。。。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1340 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 17:40 · PVG 01:40 · LAX 09:40 · JFK 12:40
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.