From 44f5f28eccc822c74a723bf89fbf62a4c72a1857 Mon Sep 17 00:00:00 2001 From: Amin Bahrami Date: Wed, 13 Feb 2019 16:38:16 +0330 Subject: [PATCH] Fix redirect bug --- .../abpdownloader/ABPDownloader.java | 16 +++++- app/src/main/AndroidManifest.xml | 4 +- .../abpdownloaderlib/ActivityMain.java | 53 +++++++++++++++++++ app/src/main/res/layout/activity_main.xml | 14 ++++- 4 files changed, 84 insertions(+), 3 deletions(-) diff --git a/ABPDownloader/src/main/java/com/aminbahrami/abpdownloader/ABPDownloader.java b/ABPDownloader/src/main/java/com/aminbahrami/abpdownloader/ABPDownloader.java index a312232..9de259e 100644 --- a/ABPDownloader/src/main/java/com/aminbahrami/abpdownloader/ABPDownloader.java +++ b/ABPDownloader/src/main/java/com/aminbahrami/abpdownloader/ABPDownloader.java @@ -205,7 +205,21 @@ public void run() Log.i("ABPDownloader","Response Code: "+responseCode); - if(responseCode==200) + // With redirect + if(followRedirect) + { + if(responseCode >= 300 && responseCode<=399) + { + Log.i("ABPDownloader","Redirect URL: "+httpURLConnection.getHeaderField("Location")); + + setSourceUrl(httpURLConnection.getHeaderField("Location")); + start(); + + return; + } + } + + if(responseCode >= 200) { totalSize=httpURLConnection.getContentLength(); diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 58ad4c2..1024a87 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,6 +2,8 @@ + + - + diff --git a/app/src/main/java/com/aminbahrami/abpdownloaderlib/ActivityMain.java b/app/src/main/java/com/aminbahrami/abpdownloaderlib/ActivityMain.java index 2b14161..caa7f49 100644 --- a/app/src/main/java/com/aminbahrami/abpdownloaderlib/ActivityMain.java +++ b/app/src/main/java/com/aminbahrami/abpdownloaderlib/ActivityMain.java @@ -2,6 +2,11 @@ import android.app.Activity; import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.widget.ProgressBar; + +import com.aminbahrami.abpdownloader.ABPDownloader; public class ActivityMain extends Activity { @@ -10,5 +15,53 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + + findViewById(R.id.download).setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View v) + { + download(); + } + }); + } + + private void download() + { + final ProgressBar progressBar=findViewById(R.id.progressBar); + + ABPDownloader downloader=new ABPDownloader(); + + downloader.setSourceUrl("http://www.14bazikon.com/14bazikon.apk") + .setDestinationPath(getFilesDir()+"/test.apk") + .setFollowRedirect(true) + .setDownloadListener(new ABPDownloader.IDownloadListener() + { + @Override + public void onProgressDownload(int percent) + { + progressBar.setProgress(percent); + } + + @Override + public void onCompleteDownload() + { + + } + + @Override + public void onError(int errorCode,String errorString) + { + Log.i("LOG","Error: "+errorString); + } + + @Override + public void onCancel() + { + + } + }) + .setFollowRedirect(true) + .start(); } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index c93b314..c6cd0b3 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -5,7 +5,19 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" + android:orientation="vertical" + android:gravity="center" tools:context=".ActivityMain"> - + + +