有时在Url上有FileNotFoundException

时间:2016-10-09 23:40:38

标签: inputstream httpurlconnection java-io

抱歉英语不好。

我的问题与此问题非常相似" 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)

MRDownloader:

        @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文件,所以如果你需要我可以添加更多。感谢。

1 个答案:

答案 0 :(得分:0)

如今,我有时间了解这一点,最后我发现了它为什么以及何时发生。我想与其他用户分享这个。 它为什么会在服务器发送错误请求时发生,并且此网站有时会发送" 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;
}