对不起,我的英语不好。
我的问题与问题"FileNotFoundException at URL“非常相似,但有时它会出现错误,不会继续,但有时它会完美地工作。这个问题是在第一,connection.What可以是导致它有时工作和有时不工作的问题?
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®=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文件,所以如果你需要,我可以添加更多。谢谢。
发布于 2016-11-05 05:10:35
如今,我已经有时间理解这一点,最终我找到了它发生的原因和时间。我想把这个分享给其他用户。这就是为什么会在服务器发送错误请求时发生,对于这个站点,有时会发送"Service Unavaiable503“,这段代码会失败。但是添加了一个条件来重复最多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;
}https://stackoverflow.com/questions/39949640
复制相似问题