2014-01-07 58 views
1

我試圖下載從URL的視頻和獲取此異常:java.net.ProtocolException:重定向過多Android中

java.net.ProtocolException: Too many redirects

下面是我的代碼。我在這裏做錯了什麼?

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:

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) 

`

+0

請在這裏發表您的logcat的。 – rup35h

+0

顯然這是一個310錯誤代碼。該錯誤可能位於您的服務器上。 – Manitoba

+0

@Manitoba我應該爲此做什麼是什麼確切的問題?你能解釋一下嗎?非常感謝您的好意。 – user3154663

回答

1

我知道這是一歲,但它出來,並沒有回答這樣..

我有同樣的問題,儘管鏈接立即在私人Firefox中打開。另外,這隻發生在一個設備上,其他3個測試設備沒有這樣的問題。

This answer worked for me

下面是代碼(我改正錯別字很少,並加入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();