【发布时间】:2016-07-20 16:18:39
【问题描述】:
我正在开发一个允许本地离线观看视频内容的安卓应用。
我正在尝试使用他们的 API 从 Vimeo 下载视频,但我遇到了一个我认为与 url 本身及其身份验证有关的问题。
我被告知该 URL 是 HTTP 302 重定向。
我已经使用来自https://archive.org/download/Popeye_forPresident/Popeye_forPresident_512kb.mp4 的大力水手视频测试了我的程序,它可以正常工作。但是当我输入 Vimeo 链接时,它甚至无法连接。
这是运行实际连接的方法。
public String DownloadFile(String fileURL, String fileName) {
InputStream in = null;
BufferedInputStream inStream = null;
FileOutputStream out = null;
HttpURLConnection connection = null;
try {
File FileDir = mContext.getExternalFilesDir(Environment.DIRECTORY_MOVIES);
URL url = new URL(fileURL);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.connect();
//Code stops here with vimeo video but not with popeye video.
...
out = new FileOutputStream(new File(FileDir, fileName + ".mp4"));
in = connection.getInputStream();
inStream = new BufferedInputStream(in, 1024 * 5);
byte[] buffer = new byte[1024 * 5];
int len;
while ((len = inStream.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
}
... //catch and finally { flush, close and disconnection}
return null;
}
这是设置 url 并执行它的异步任务。
class DownloadFileFromURL extends AsyncTask<String, String, String> {
...
@Override
protected String doInBackground(final String... args) {
//If it is a normal direct link such as site.com/video.mp4 (the popeye video)
//Set link to site.com/video.mp4 and save as "args[1]" on disk.
if(FilenameUtils.isExtension(args[0], "mp4")){
DownloadFile(args[0], args[1]);
}
//Else if a vimeo link (which it is)
else if (args[0].contains("vimeo")){
final int pos = Integer.parseInt(args[2]);
//Use Vimeo API to get the videos from Vimeo.
VimeoClient.getInstance().fetchNetworkContent(CHANNELS_VIDEO_URI, new ModelCallback<VideoList>(VideoList.class) {
@Override
public void success(VideoList videoList) {
if (videoList != null && videoList.data != null && !videoList.data.isEmpty()) {
Video video = videoList.data.get(pos);
ArrayList<VideoFile> videoFiles = video.files;
if(videoFiles != null && !videoFiles.isEmpty()) {
VideoFile videoFile = videoFiles.get(0);
//Set link to the link that you retrieve.
//Example: http://player.vimeo.com/external/175231540.hd.mp4?s=78206f99af10e32354851f6ca62252782b1393fb&profile_id=174&oauth2_token_id=903072961
String link = videoFile.link;
DownloadFile(link, args[1]);
}
}
}
@Override
public void failure(VimeoError error) {
Toast.makeText(mContext, "Failure in VideoList Reading", Toast.LENGTH_LONG).show();
}
});
}
return null;
}
...
}
但是链接http://player.vimeo.com/external/175231540.hd.mp4?s=78206f99af10e32354851f6ca62252782b1393fb&profile_id=174&oauth2_token_id=903072961 没有连接到。我尝试在 hd.mp4 之后删除所有内容,但我认为这不起作用,因为在浏览器中访问该链接会导致出现授权被拒绝错误。
如何连接到此链接并使用令牌从 HTTP 302 重定向中获取文件?
【问题讨论】: