我正在尝试从URL下载视频并得到以下异常:
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();
}
}
日志猫:
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)
'
我知道
这是一年前的事情了,但它出来了,没有答案。
我遇到了同样的问题,尽管该链接立即在私人火狐中打开。此外,这仅在一台设备上发生,其他 3 台测试设备没有这样的问题。
这个答案对我有用:
这是代码(我纠正了一些拼写错误并添加了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();