【问题标题】:Sometimes FileNotFoundException at Url有时在 URL 处出现 FileNotFoundException
【发布时间】:2017-02-18 08:46:12
【问题描述】:

抱歉英语不好。

我的问题与这个问题“FileNotFoundException at URL”非常相似 但有时它会出错并且不会继续,但有时它会完美运行。这个问题是第一次连接。可能是什么问题导致它有时工作有时不工作?

10-10 02:19:41.128    9667-9819/? W/System.err﹕ java.io.FileNotFoundException: http://cdn59.my.mail.ru/v/59908302.mp4?slave[]=s%3Ahttp%3A%2F%2Fvideo-cephgw1.i%3A8080%2Frados%2F59908302-v&p=f&expire_at=1476068400&touch=1475880462&reg=76&sign=dd01f023e682705a105440ab5e93f5cb38cfeadd
10-10 02:19:41.128    9667-9819/? W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:186)
10-10 02:19:41.128    9667-9819/? W/System.err﹕ at makgun.kturk.MRDownloader$1.doInBackground(MRDownloader.java:105)
10-10 02:19:41.128    9667-9819/? W/System.err﹕ at makgun.kturk.MRDownloader$1.doInBackground(MRDownloader.java:39)
10-10 02:19:41.128    9667-9819/? W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
10-10 02:19:41.128    9667-9819/? W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
10-10 02:19:41.128    9667-9819/? W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
10-10 02:19:41.128    9667-9819/? W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
10-10 02:19:41.128    9667-9819/? W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
10-10 02:19:41.128    9667-9819/? W/System.err﹕ at java.lang.Thread.run(Thread.java:841)

MR下载器:

        @Override
        protected String doInBackground(String... urls) {

            try {
                String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" +name;
                File file = new File(path);
                int total;
                if (file.exists())total=Integer.parseInt(String.valueOf(file.length()));
                else total=0;
                //New Added
                if (urls[0].startsWith("//"))urls[0]="http:"+urls[0];
                //New Added finished
                URL url = new URL(urls[0]);
                HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
                OutputStream output = new FileOutputStream(path, true);
                urlConnection.addRequestProperty("Range", "bytes=" + file.length() + "-");
                if(!Cookie.isEmpty())
                urlConnection.addRequestProperty("Cookie", "video_key=" +Cookie);
                urlConnection.setRequestProperty("User-Agent","Mozilla/5.0");
                urlConnection.setRequestProperty("Accept","*/*");
                urlConnection.setConnectTimeout(7000);
                urlConnection.setReadTimeout(7000);
                urlConnection.connect();

                int lenghtOfFile = urlConnection.getContentLength();
                PD.setMax(lenghtOfFile+total);
                InputStream input = new BufferedInputStream(urlConnection.getInputStream());
                byte data[] = new byte[1024];
                //    int total=Integer.parseInt(String.valueOf(file.length()));
                int count;
                Log.d("MakgunLENGHT-OF-FILE", Integer.toString(lenghtOfFile));

                while ((count = input.read(data)) != -1) {
                    total += count;
                    PD.setProgress(total);
                    output.write(data, 0, count);
                    if (pause){output.flush();output.close();input.close();if (PD.isShowing()) PD.dismiss();break;}
                }
                output.flush();
                output.close();
                input.close();

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            return null;
        }

您可以使用此链接进行尝试。 TryLink。 当您请求此链接时,它会生成“video_key”cookie,并且响应正文包含您需要的所有信息,例如视频的流 URL。我不会复制所有的 java 文件,所以如果你需要我可以添加更多。谢谢。

【问题讨论】:

  • 现在,我有时间了解这一点,最后我找出了它的原因和时间。我想与其他用户分享这个。其原因是每当服务器发送错误请求并且此站点有时会发送 Service Unavaible 503 并且此代码失败时,都会发生这种情况。但是添加一个条件以重复最多 10 次它就像一个魅力。至少单击一次就足够了,它最多重复 10 次,通常在 2-3 次尝试后它会从服务器获得 2xx 响应代码。如果收到 2xx 代码,它将中断循环。它不再重复。这解决了我的问题。

标签: inputstream httpurlconnection java-io


【解决方案1】:

现在,我有时间了解这一点,最后我发现了它为什么以及何时发生。我想与其他用户分享这个。 这是为什么每当服务器发送错误请求并且此站点有时会发送 “Service Unavaible 503” 并且此代码失败时,都会发生这种情况。但是添加条件最多重复 10 次它就像一个魅力。至少单击一次就足够了,最多重复 10 次,一般尝试 2-3 次后从服务器获得 2xx 响应代码。如果收到 2xx 代码,它将中断循环。它不再重复。这解决了我的问题。

    int x = 1;
while (true) {
    //otherStuffs...
    if (responseCode == 503) {
    Log.d("makgunResponseCode503", "Now ResponseCode is 503 repeating " + x + " times");
    x++;
    }
    else
    {Log.d("makgunResponseCode", "ResponseCode is: " + responseCode);break}
    if (x > 10) break;
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-15
  • 1970-01-01
  • 2015-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-19
相关资源
最近更新 更多