我正在尝试制作一个可以通过 youtube url 下载 mp3 文件的应用程序。
我对 youtube-mp3.org api 做了一些研究,我认为应该这样做:
- 得到
http://www.youtube-mp3.org/api/pushItem/?item=http://www.youtube.com/watch?v=xo9EV3A4oaA&xy=yx
- 第 1 步返回一个 ID,您必须在以下请求中使用该 ID:
"http://www.youtube-mp3.org/api/itemInfo/?video_id=" + ID
- 第 2 步返回另一个代码,您必须在此请求中使用该代码:
"http://www.youtube-mp3.org/get?video_id=xo9EV3A4oaA&h=" + <code from step 2>
- 第 3 步重新运行 mp3。
不幸的是,我的代码在第 1 步已经失败:我得到一个 404,找不到页面。
这是我的代码(仅适用于第 1 步):
private DefaultHttpClient createHttpClient() {
HttpParams my_httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(my_httpParams, 3000);
HttpConnectionParams.setSoTimeout(my_httpParams, 15000);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
ThreadSafeClientConnManager multiThreadedConnectionManager = new ThreadSafeClientConnManager(my_httpParams, registry);
DefaultHttpClient httpclient = new DefaultHttpClient(multiThreadedConnectionManager, my_httpParams);
return httpclient;
}
private class DownloadVid extends AsyncTask<Void, Void, Void> {
int mStatusCode = 0;
String content = "";
@Override
protected Void doInBackground(Void... args) {
String url = "http://www.youtube-mp3.org/api/pushItem/?item=http://www.youtube.com/watch?v=xo9EV3A4oaA&xy=yx";
DefaultHttpClient httpclient = createHttpClient();
HttpGet httpget = new HttpGet(url);
httpget.addHeader("Accept-Location", "*");
try {
HttpResponse response = httpclient.execute(httpget);
StatusLine statusLine = response.getStatusLine();
mStatusCode = statusLine.getStatusCode();
if (mStatusCode == 200){
content = EntityUtils.toString(response.getEntity());
}
} catch (ClientProtocolException e) {
e.printStackTrace();
mStatusCode = 0;
} catch (IOException e) {
e.printStackTrace();
mStatusCode = 0;
} catch (IllegalStateException e){
e.printStackTrace();
mStatusCode = 0;
}
return null;
}
@Override
protected void onPostExecute(Void arg) {
mProgressDialog.dismiss();
Toast.makeText(MainActivity.this, "Result=" + content + " StatusCode=" + mStatusCode, Toast.LENGTH_LONG).show();
}
}
我不确定为什么它不起作用。有任何想法吗?