HttpURLConnection
クラスの助けを借りて接続を開こうとしています。HTTP URL接続がリダイレクトを処理できません
URLを処理するためにこのコードを試しましたが、ログを見るとリダイレクトされたURLを取得できません。サンプルコードでは、URLを取得するために、while
ループを使用していることがわかります。これにより、同じURLが記録されます。私は熱心に問題のより良い修正を探しています。問題がはっきりすることを願っています。ここで
は、私が使用しているサンプルコードです: - ここに
Log.d(TAG,"URL received : --- >"+downloadURL);
URL url=new URL(downloadURL);
HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
httpURLConnection.setDoInput(true);
httpURLConnection.setInstanceFollowRedirects(true);
httpURLConnection.connect();
Log.d(TAG,httpURLConnection.getResponseMessage()+" Append with "+httpURLConnection.getResponseCode());
while(httpURLConnection.getResponseCode()==HttpURLConnection.HTTP_MOVED_PERM)
{
httpURLConnection.getInputStream();
URL url1=httpURLConnection.getURL();
Log.d(TAG,"Redirected URL = "+url1);
}
InputStream inptStream=httpURLConnection.getInputStream();
はlogcatの出力である: - HttpURLConnection
クラスで
03-08 13:28:38.814 22597-23668/abhirojpanwar.tablayout D/AsyncImageDownloader: Redirected URL = https://unsplash.com/photos/65sru5g6xHk/download
03-08 13:28:38.814 22597-23668/abhirojpanwar.tablayout D/AsyncImageDownloader: Redirected URL = https://unsplash.com/photos/65sru5g6xHk/download
03-08 13:28:38.814 22597-23668/abhirojpanwar.tablayout D/AsyncImageDownloader: Redirected URL = https://unsplash.com/photos/65sru5g6xHk/download
03-08 13:28:38.814 22597-23668/abhirojpanwar.tablayout D/AsyncImageDownloader: Redirected URL = https://unsplash.com/photos/65sru5g6xHk/download
03-08 13:28:38.814 22597-23668/abhirojpanwar.tablayout D/AsyncImageDownloader: Redirected URL = https://unsplash.com/photos/65sru5g6xHk/download
03-08 13:28:38.814 22597-23668/abhirojpanwar.tablayout D/AsyncImageDownloader: Redirected URL = https://unsplash.com/photos/65sru5g6xHk/download
03-08 13:28:38.814 22597-23668/abhirojpanwar.tablayout D/AsyncImageDownloader: Redirected URL = https://unsplash.com/photos/65sru5g6xHk/download
03-08 13:28:38.814 22597-23668/abhirojpanwar.tablayout D/AsyncImageDownloader: Redirected URL = https://unsplash.com/photos/65sru5g6xHk/download
03-08 13:28:38.814 22597-23668/abhirojpanwar.tablayout D/AsyncImageDownloader: Redirected URL = https://unsplash.com/photos/65sru5g6xHk/download
03-08 13:28:38.815 22597-23668/abhirojpanwar.tablayout D/AsyncImageDownloader: Redirected URL = https://unsplash.com/photos/65sru5g6xHk/download
03-08 13:28:38.815 22597-23668/abhirojpanwar.tablayout D/AsyncImageDownloader: Redirected URL = https://unsplash.com/photos/65sru5g6xHk/download
03-08 13:28:38.815 22597-23668/abhirojpanwar.tablayout D/AsyncImageDownloader: Redirected URL = https://unsplash.com/photos/65sru5g6xHk/download
03-08 13:28:38.815 22597-23668/abhirojpanwar.tablayout D/AsyncImageDownloader: Redirected URL = https://unsplash.com/photos/65sru5g6xHk/download
申し訳ありませんが、私はそれを理解していないかもしれませんが、正確にはリダイレクトされたURLで200応答を取得していますか? –
デフォルトでは、 'setFollowRedirects'はtrueです。つまり、自動的に新しいURLにリダイレクトされ、それから受け取ったレスポンス(200)を返します。 –
これをfalseに設定すると、URLリダイレクトをチェックしないように設定しました。これは後で自分自身をループの中で実行します。 –