简体   繁体   English

java.net.ProtocolException:Android中的重定向过多

[英]java.net.ProtocolException: Too many redirects in Android

I am trying to download a video from a URL and getting this exception: 我正在尝试从URL下载视频并遇到以下异常:

java.net.ProtocolException: Too many redirects

Below is my code. 下面是我的代码。 What am I doing wrong here? 我在这里做错了什么?

private void savePrivateExternalFile(String fileURL, String fName)
{
    CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
    HttpURLConnection connection = null;
    URL url = null;
    long startTime = System.currentTimeMillis();

    try
    {
        url = new URL(fileURL);
        connection = (HttpURLConnection) url.openConnection();
        connection.addRequestProperty(BsharpConstant.WEB_SERVICES_COOKIES, cookie);
        connection.setDoOutput(true);
        connection.connect();

        File folderDir = null;
        if (clickedItemId == 0) folderDir = new File(getExternalFilesDir("Product") + "/Brochure");
        else folderDir = new File(getExternalFilesDir("Product") + "/Videos");

        File file = new File(folderDir, fName);

        if (file.exists()) file.delete();

        if (folderDir.isDirectory() || folderDir.mkdirs())
        {
            InputStream inputStream = connection.getInputStream(); // Exception is thrown here !!
            FileOutputStream fileOutputStream = new FileOutputStream(folderDir + "/" + fName);

            int len = 0;
            byte[] buffer = new byte[1024];

            if (clickedItemId == 1)
            {
                while ((le = inputStream.read(buffer)) != -1)
                {
                    fileOutputStream.write(buffer, 0, len);
                }
            }
            else
            {
                BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream, 1024 * 50);
                while ((len = bufferedInputStream.read(buffer)) != -1)
                {
                    fileOutputStream.write(buffer, 0, len);
                }
                bufferedInputStream.close();
                Log.i("Download", "download completed in " + ((System.currentTimeMillis() - startTime) / 1000) + " sec");
            }

            fileOutputStream.close();
            inputStream.close();

            editSharedPreferences.putString(fName, fName);
            editSharedPreferences.commit();
        }
        else
        {
            Toast.makeText(getApplicationContext(), BsharpUserMessage.UNABLE_TO_CREATE_THE_FOLDER, Toast.LENGTH_LONG).show();
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

LogCat: logcat的:

01-07 14:21:36.163: W/System.err(25391): java.net.ProtocolException: Too many redirects
01-07 14:21:36.163: W/System.err(25391):    at l  libcore.net.http.HttpURLConnectionImpl.processResponseHeaders(HttpURLConnectionImpl.java:368)
01-07 14:21:36.163: W/System.err(25391):    at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:292)
01-07 14:21:36.163: W/System.err(25391):    at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
01-07 14:21:36.173: W/System.err(25391):    at com.abc.xyz.ProductBrochureActivity.savePrivateExternalFile(ProductBrochureActivity.java:231)
01-07 14:21:36.173: W/System.err(25391):    at com.abc.xyz.ProductBrochureActivity.access$7(ProductBrochureActivity.java:200)
01-07 14:21:36.173: W/System.err(25391):    at com.abc.xyz.ProductBrochureActivity$2.run(ProductBrochureActivity.java:140)
01-07 14:21:36.173: W/System.err(25391):    at java.lang.Thread.run(Thread.java:856)

` `

I know this is one year old, but it comes out and has no answer so.. 我知道这已经一岁了,但是它出来了,所以没有答案。

I had the same issue, although the link was opening in private firefox immediately. 我遇到了同样的问题,尽管该链接立即在私有firefox中打开。 Also, this was happening only on one device, the other 3 testing devices had no such problem. 此外,这仅在一个设备上发生,其他3个测试设备也没有这种问题。

This answer worked for me : 这个答案对我有用

Here is the code(I corrected few typos and added StringBuilder instead of the concatenation): 这是代码(我纠正了一些拼写错误,并添加了StringBuilder而不是连接):

        HttpClient httpClient = new DefaultHttpClient();
        httpClient.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true); 
        HttpGet httpGet = new HttpGet(webLink);
        HttpResponse response = httpClient.execute(httpGet);
        StringBuilder responseBuilder = new StringBuilder();
        BufferedReader buffer = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        String s = "";
        while ((s = buffer.readLine()) != null) 
            responseBuilder.append(s);

        return responseBuilder.toString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.net.ProtocolException:重定向过多:21 Android 应用 - java.net.ProtocolException: Too many redirects: 21 Android App java.net.ProtocolException: 服务器重定向次数过多 (20) - java.net.ProtocolException: Server redirected too many times (20) java.net.ProtocolException: 服务器重定向次数过多 - java.net.ProtocolException: Server redirected too many times android中带有HttpsURLConnection的java.net.protocolException - java.net.protocolException with HttpsURLConnection in android java.net.Authenticator:java.net.ProtocolException:服务器重定向太多次(20) - java.net.Authenticator : java.net.ProtocolException: Server redirected too many times (20) 获取“java.net.ProtocolException:服务器重定向次数太多”错误 - Getting “java.net.ProtocolException: Server redirected too many times” Error android java.net.ProtocolException:已建立连接 - android java.net.ProtocolException: Connection already established Android Getting = java.net.ProtocolException:写入OutpuStream时超出了内容长度限制,即0个字节 - Android Getting = java.net.ProtocolException: exceeded content-length limit of 0 bytes, when writing to OutpuStream java.net.ProtocolException: 方法不支持请求正文:GET - java.net.ProtocolException: method does not support a request body: GET 改进:java.net.ProtocolException:未知方法“ PATCH” - Retrofit: java.net.ProtocolException: Unknown method 'PATCH'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM