简体   繁体   English

有时会抛出FileNotFoundException,我不知道为什么

[英]FileNotFoundException is being thrown sometimes and i don't know why

I am trying to run a webservice that return XML format but it sometimes throws FileNotFoundException on 3G network and sometimes works successfully on WIFI network 我正在尝试运行返回XML格式的Web服务,但有时会在3G网络上引发FileNotFoundException,有时会在WIFI网络上成功运行

here is the code of the connection : 这是连接的代码:

      @Override
    protected ArrayList<NewsItemVO> doInBackground(Void... args) {


        try {
            URLConnection conn = new URL(feedUrl).openConnection();
            conn.setConnectTimeout(20000);
            conn.setReadTimeout(20000);
            InputStream in = new BufferedInputStream(conn.getInputStream());
          }
}

and this is the line that throws the exception 这是引发异常的行

 InputStream in = new BufferedInputStream(conn.getInputStream());

Before using input stream you could check if it is available or not? 在使用输入流之前,您可以检查输入流是否可用?

http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html

As per documentation : 根据文档:

Signals that an attempt to open the file denoted by a specified pathname has failed. 表示尝试打开由指定路径名表示的文件失败。

This exception will be thrown by the FileInputStream, FileOutputStream, and RandomAccessFile constructors when a file with the specified pathname does not exist. 当具有指定路径名的文件不存在时,FileInputStream,FileOutputStream和RandomAccessFile构造函数将抛出此异常。 It will also be thrown by these constructors if the file does exist but for some reason is inaccessible, for example when an attempt is made to open a read-only file for writing. 如果该文件确实存在,但由于某种原因而无法访问,例如,当试图打开一个只读文件进行写入时,这些构造方法也会抛出该文件。

Since your code is working for most of the times and not working for some times, I suspect that you are getting ConnectionTimeOut. 由于您的代码在大多数情况下都可以正常工作,而在某些时候却无法正常工作,因此我怀疑您正在获得ConnectionTimeOut。

To dig the issue further, please follow below steps. 要进一步研究问题,请按照以下步骤操作。

1) Declare a long variable t1 and initialize it to System.currentTimeMillis() before try block. 1)在尝试块之前声明一个长变量t1并将其初始化为System.currentTimeMillis()。 declare one more long varaible t2 and get current time after finally block. 声明一个更长的变量t2并在最终阻止之后获取当前时间。 Check the difference. 检查差异。 If the difference is more than connection timeout setting, we can conclude that you are getting connection time outs. 如果差异大于连接超时设置,我们可以得出结论,您正在获得连接超时。

2) Print complete exception stack trace in catch clause. 2)在catch子句中打印完整的异常堆栈跟踪。

3) When you get this exception, check the permissions on file and confirm that file is accessible with read/execute permissions. 3)当您收到此异常时,请检查文件的权限,并确认具有读取/执行权限的文件可以访问。

And one more thing - Since it is working fine in wifi and not working in 3G, I suspect connection timeout is the root cause. 还有一件事-由于它在wifi中可以正常工作,而在3G中不能正常工作,我怀疑连接超时是根本原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 javax.imageio.IIOException:无法读取输入文件,被抛出? 但我不知道为什么会这样? - javax.imageio.IIOException: Can't read input file! is being thrown, but I don't know why it is? 不会引发FileNotFoundException - FileNotFoundException not being thrown 当我在AllJoyn Framework上设计具有多个返回值的接口时,调用将引发异常。 我不知道为什么 - When I designed an interface with multiple return valuse on AllJoyn Framework, an exception is thrown by the call. I don't know why 遇到异常,我不知道为什么 - Getting exception and I don't know why 我不知道为什么 null 值 - I don't know why the null value 我不明白为什么抛出这个 ClassNotFoundException - I don't understand why this ClassNotFoundException gets thrown 交换机正在以红色取消,我不知道原因 - Switch is being undelined in red and i don't know the reason 有时我不知道某些方法属于哪些类。 我究竟做错了什么? - Sometimes I don't know which classes certain methods belong to. What am I doing wrong? transactionTemplate不适用于mybatis,但我不知道为什么 - transactionTemplate doesn't work with mybatis but I don't know why Repaint()无法正常工作,我也不知道为什么 - Repaint() isn't working and I don't know why
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM