2015-07-19 19 views
-1

我有以下的Java代碼UrlConnection.getInputStream()返回一個例外,那就是空在Java中

try { 
    String u = "http://webapi.com/demo.zip"; 
    URL url = new URL(u); 
    URLConnection ucon = url.openConnection(); 
    InputStream is = ucon.getInputStream(); 
} 
catch (Exception e) { 
    Log.d('downloaderror', e.getMessage()); 
} 

但由於某些原因,InputStream is = ucon.getInputStream()會導致錯誤,catch塊被激發。當catch塊被觸發時,e的值是null

有沒有人知道我的ucon.getInputStream()有什麼問題?我知道http://webapi.com/demo.zip存在,因爲我可以從我的Web瀏覽器下載該文件。

編輯 這裏有ucon.getInputstream()

java.lang.Exception 
      at com.example.instantramenz.samplewebview.MainActivity$1.onClick(MainActivity.java:94) 
      at android.view.View.performClick(View.java:4633) 
      at android.view.View$PerformClick.run(View.java:19270) 
      at android.os.Handler.handleCallback(Handler.java:733) 
      at android.os.Handler.dispatchMessage(Handler.java:95) 
      at android.os.Looper.loop(Looper.java:146) 
      at android.app.ActivityThread.main(ActivityThread.java:5602) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:515) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
      at dalvik.system.NativeStart.main(Native Method) 
+0

的'價值e'是否爲空或者打印的字符串是否包含文本'null'(即消息是'null')? –

+0

e的值爲空。然後null.getMessage()導致應用程序崩潰後 – John

+0

請發佈堆棧跟蹤。如果我沒有記錯的話,catch-block中的異常永遠不能爲null。 – Brian

回答

1

我放上這行代碼下載文件前修復該問題的堆棧跟蹤:

  android.os.StrictMode.ThreadPolicy policy = new android.os.StrictMode.ThreadPolicy.Builder().permitAll().build(); 
      android.os.StrictMode.setThreadPolicy(policy);