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)
`
請在這裏發表您的logcat的。 – rup35h
顯然這是一個310錯誤代碼。該錯誤可能位於您的服務器上。 – Manitoba
@Manitoba我應該爲此做什麼是什麼確切的問題?你能解釋一下嗎?非常感謝您的好意。 – user3154663