【问题标题】:Could'nt download file on Android while on PC it downloads ok (amazon)无法在 Android 上下载文件,而在 PC 上下载正常(亚马逊)
【发布时间】:2014-07-20 00:25:56
【问题描述】:

遇到了一个奇怪的问题。带有 URL 的文件:https://s3.amazonaws.com/myappdata/msg/171401089927.mp3(不再可用)在 PC 及其 mp3 文件上下载正常。但是当我尝试在 Android FOA 上对其进行 DL 时,我得到的是内容类型“application/xml”而不是“audio/mpeg”,当我开始下载时,我得到:

05-30 12:13:44.478: E/PlayerService(28023): java.io.FileNotFoundException: https://s3.amazonaws.com/myappdata/msg/171401089927.mp3
05-30 12:13:44.478: E/PlayerService(28023):     at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
05-30 12:13:44.478: E/PlayerService(28023):     at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:270)

用于DL的代码:

    /**
     * Download the url stream to a temporary location
     */
    public void downloadAudioIncrement(String mediaUrl) throws IOException {
        Log.i(TAG, "downloadAudioIncrement(): mediaUrl: "+mediaUrl+"\ncacheDir: "+cacheDir);

        URL url = null;

        try {
            url = new URL(mediaUrl);
        } catch (MalformedURLException e) {
            e.printStackTrace();
            throw new IOException("Unable to create InputStream for mediaUrl:" + mediaUrl);
        }

        // this file will represent whole downloaded song
        mp3FileDownloaded = new File(cacheDir, mp3FileName);
        if (!mp3FileDownloaded.exists())
            //FileUtils.makeDirsForFile(mp3FileDownloaded);
            try{
                mp3FileDownloaded.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
        }

        if (!mp3FileDownloaded.canWrite())
            throw new IOException("Can't open temporary file for writing");

        HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();

        urlConnection.setReadTimeout(1000 * 20);
        urlConnection.setConnectTimeout(1000 * 5);

        urlConnection.setDoInput(true);
        urlConnection.setDoOutput(true);

        int mp3BytesSize = urlConnection.getContentLength();
        // final String
        // contentLengthStr=urlConnection.getHeaderField("content-length");

        String ctype = urlConnection.getContentType();
        if (ctype == null) {
            ctype = "";
        } else {
            ctype = ctype.toLowerCase(Locale.US);
        }
        // See if we can handle this type
        Log.i(TAG, "Content Type: " + ctype);

        if ( ctype.contains("audio/mpeg") || TextUtils.isEmpty(ctype) ) {

            String temp = urlConnection.getHeaderField(BITRATE_HEADER);
            Log.i(TAG, "Bitrate: " + temp);
            // if (temp != null){
            // bitrate = new Integer(temp).intValue();
            // }
        } else {
            Log.e(TAG, UNSUPPORTED_AUDIO_TYPE+": " + ctype);
//          throw new IOException(UNSUPPORTED_AUDIO_TYPE+": " + ctype);
            // Log.e(TAG, "Or we could not connect to audio");
            // stop();
            // return;
        }
        final InputStream stream = new BufferedInputStream(urlConnection.getInputStream(),8192);
...

就在最后显示的代码行(实例化 InputStream 流)处,提到了 IOExeption。还有其他 mp3 文件存在于同一位置,它们正在下载没有任何问题,但只是上面提到的 url 失败。
这里可能有什么问题?
更新
看起来这个问题发生在 HTC Rezound 与 AOS 4.0.4 上。在其他设备上,使用 AOS 2.3.5 一切正常。

【问题讨论】:

    标签: android http download


    【解决方案1】:

    好像这条线

    urlConnection.setDoOutput(true);
    

    是问题的根源,因为我没有上传任何数据。一切正常,因为我会评论它。这些FileNotFoundException while getting the InputStream object from HttpURLConnectionAndroid HttpUrlConnection getInputStream throws NullPointerException 线程也可能会有所帮助。

    【讨论】:

      猜你喜欢
      • 2018-01-22
      • 2012-08-23
      • 2014-06-06
      • 2012-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-04
      • 1970-01-01
      相关资源
      最近更新 更多