2

我有一个应用程序,它在正常情况下从云存储加载和播放音乐没有问题,除了Dropbox短链接。这些链接使用 302 标头重定向到 https 链接:-

302 Found 该资源在 https://www.dropbox.com/s/jhzh3woy3qxblck/05%20-%20Cinema.ogg找到;你应该被自动重定向。

此代码的结构是双重静态方法,首先 [按时间顺序] 查找重定向,然后将数据获取到文件。

我试图让它工作,因为第二个链接目前让我从 Dropbox 下载了很多不相关的 HTML,而不是所需的文件本身!

/**
 * Return an Audio File from a URL String
 * throws IOException
 * 
 * @param url the URL which provides the target
 * @return File - audio file
 */
public static File getAudioFile(String urlString, File f) {
    String newUrl = null;
    try {
        newUrl = getRedirect(urlString);
    } catch (ClientProtocolException e) {
        Log.e(TAG, "ClientProtocolException Error getting Redirect ", e);
    } catch (IOException e) {
        Log.e(TAG, "IOException Error getting Redirect", e);
    }
    if (newUrl != null) {
        urlString = newUrl;
    }
    else {
        Log.i(TAG, "IOException Error getting Redirect because its null");
    }

    try {
        URL url = new URL(urlString);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setInstanceFollowRedirects(true);
        conn.setReadTimeout(40 * 1000);
        conn.setConnectTimeout(45 * 1000);
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Connection", "close");
        conn.setDoInput(true);
        conn.setDefaultUseCaches(true);
        // Starts the input from the URL
        conn.connect();
        InputStream is = conn.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        BufferedOutputStream bof = new BufferedOutputStream(new FileOutputStream(f));

        // write the inputStream to the FileOutputStream
        byte [] bytes = new byte [409600];
        int block;
        while((block = bis.read(bytes, 0, bytes.length)) > 0) {
            bof.write(bytes, 0, block);
            Log.d(TAG, "Write a block of " + block);
        }

        bof.flush();
        bof.close();
        bis.close();
        is.close();
        conn.disconnect();
    } catch (Exception e) {
        Log.e(TAG, "Error getting Audio File", e);
    }
    return f;
}
/**
 * Get the new url from a http redirect 
 * throws IOException
 * 
 * @param url the URL which provides the target
 * @return url - the single url redirect
 */
public static String getRedirect(String urlString) throws ClientProtocolException, IOException {
    HttpParams httpParameters = new BasicHttpParams();
    HttpClientParams.setRedirecting(httpParameters, false);

    HttpClient httpClient = new DefaultHttpClient(httpParameters);      
    HttpGet httpget = new HttpGet(urlString);
    HttpContext context = new BasicHttpContext();

    HttpResponse response = httpClient.execute(httpget, context);

    // If we didn't get a '302 Found' we aren't being redirected.
    if (response.getStatusLine().getStatusCode() != HttpStatus.SC_MOVED_TEMPORARILY)
        throw new IOException(response.getStatusLine().toString());

    Header loc[] = response.getHeaders("Location");
    return loc.length > 0 ? loc[loc.length -1].getValue() : null;
}
4

1 回答 1

3

我遇到了同样的问题。我发现我可以从urlConnection.getHeaderField("Location"); 请参考下面的代码。

{
    url = new URL(urlString);
    urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.connect();
    String ResponseCode = urlConnection.getResponseCode();
    String ContentType = urlConnection.getContentType();
    if ( result.ResponseCode == HttpURLConnection.HTTP_MOVED_TEMP || result.ResponseCode == HttpURLConnection.HTTP_MOVED_PERM )
    {
       String Location = urlConnection.getHeaderField("Location");
    }
}

问候,杰克

于 2013-05-07T11:37:50.080 回答