我正在尝试从网址下载视频并获得此例外:
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 :(得分:1)
我知道这是一岁,但它出来了,没有答案所以..
我遇到了同样的问题,虽然链接是在私有firefox中立即打开的。 此外,这只发生在一台设备上,其他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();