无法建立HttpUrlConnection

时间:2016-01-16 23:01:29

标签: android http exception httpurlconnection

我通过HttpUrlConnection以块的形式下载1gb文件。服务器是我的个人电脑与Apache服务器,我正在使用Android 4.1.2在Galaxy SII上下载文件。使用DownloadManager下载工作正常,但是当我尝试使用HttpUrlConnection时,我得到了一个例外。这是我的代码:

try{
        Uri uri = Uri.parse("http://192.168.1.110/1000mb.test");
        URL url = new URL( uri.toString());
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.setReadTimeout(10000 /* milliseconds */);
        urlConnection.setConnectTimeout(15000 /* milliseconds */);
        urlConnection.setChunkedStreamingMode(104857600);
        urlConnection.connect();

        File sdcard = Environment.getExternalStorageDirectory();
        File file = new File(sdcard, "filename.ext");

        FileOutputStream fileOutput = new FileOutputStream(file);
        InputStream inputStream = urlConnection.getInputStream();

        byte[] buffer = new byte[1024];
        int bufferLength = 0;

        while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
            fileOutput.write(buffer, 0, bufferLength);
        }
        fileOutput.close();

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

我得到的例外:

E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                     java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                                         at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:275)
                                                                                         at android.view.View.performClick(View.java:4232)
                                                                                         at android.view.View$PerformClick.run(View.java:17298)
                                                                                         at android.os.Handler.handleCallback(Handler.java:615)
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                                         at android.os.Looper.loop(Looper.java:137)
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:4921)
                                                                                         at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                         at java.lang.reflect.Method.invoke(Method.java:511)
                                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
                                                                                         at dalvik.system.NativeStart.main(Native Method)
                                                                                      Caused by: java.lang.reflect.InvocationTargetException
                                                                                         at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                         at java.lang.reflect.Method.invoke(Method.java:511)
                                                                                         at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
                                                                                         at android.view.View.performClick(View.java:4232) 
                                                                                         at android.view.View$PerformClick.run(View.java:17298) 
                                                                                         at android.os.Handler.handleCallback(Handler.java:615) 
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:92) 
                                                                                         at android.os.Looper.loop(Looper.java:137) 
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:4921) 
                                                                                         at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                         at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027) 
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) 
                                                                                         at dalvik.system.NativeStart.main(Native Method) 
                                                                                      Caused by: android.os.NetworkOnMainThreadException
                                                                                         at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
                                                                                         at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
                                                                                         at libcore.io.IoBridge.connectErrno(IoBridge.java:144)
                                                                                         at libcore.io.IoBridge.connect(IoBridge.java:112)
                                                                                         at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
                                                                                         at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
                                                                                         at java.net.Socket.connect(Socket.java:842)
                                                                                         at libcore.net.http.HttpConnection.<init>(HttpConnection.java:76)
                                                                                         at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
                                                                                         at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
                                                                                         at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
                                                                                         at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
                                                                                         at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
                                                                                         at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
                                                                                         at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
                                                                                         at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
                                                                                         at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
                                                                                         at com.example.mateusz_rzepczyk.wifitester.MainActivity.addListenerOnButton1000(MainActivity.java:383)
                                                                                         at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                         at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                                         at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270) 
                                                                                         at android.view.View.performClick(View.java:4232) 
                                                                                         at android.view.View$PerformClick.run(View.java:17298) 
                                                                                         at android.os.Handler.handleCallback(Handler.java:615) 
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:92) 
                                                                                         at android.os.Looper.loop(Looper.java:137) 
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:4921) 
                                                                                         at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                         at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027) 
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) 
                                                                                         at dalvik.system.NativeStart.main(Native Method)

我注意到了什么: 1)无论我设置的连接超时值是多少,我都会得到异常 2)urlConnection.connected始终为false 3)urlConnection响应代码总是= -1

我需要实现的目标: 以100mb的块下载1gb文件并将其保存到设备存储中。

请帮我解决这个问题。我是Java的新手。

0 个答案:

没有答案