Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
getInputStream()返回在Java中为null的异常_Java_Inputstream - Fatal编程技术网

getInputStream()返回在Java中为null的异常

getInputStream()返回在Java中为null的异常,java,inputstream,Java,Inputstream,我有以下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()); }

我有以下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()上的堆栈跟踪


我通过在下载文件之前放置这行代码修复了此问题:

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

e
的值为
null
或打印的字符串包含文本
null
(即消息为
null
)?e的值为null。然后null.getMessage()会导致应用程序在发布堆栈跟踪后崩溃。如果我没记错的话,catch块中的异常永远不会是
null
。如果将
null
作为异常参数的值,则会遇到更大的问题。我将重新打开,但请尝试@ElliottFrisch的建议,并使用不同的http客户端,您可以轻松检查状态代码和响应正文。1。没有像
InputStream.getInputStream()
这样的方法。2.异常被抛出,而不是返回。3.抛出的异常从不为null,但它们的消息可以为null。4.您应该记录异常本身,而不仅仅是它的消息。
            android.os.StrictMode.ThreadPolicy policy = new android.os.StrictMode.ThreadPolicy.Builder().permitAll().build();
            android.os.StrictMode.setThreadPolicy(policy);