Question

I am very new with android and doing this first time. I am trying to download pdf form url in my app but its not getting downloaded. I really got messed up with this. I don't what i am missing ,why this is not working for me. Please help me to do this.

Here i am pasting my code:

public class ProductBrochureActivity extends Activity {
private static String cookie;
private static String nid;
WebView webViewForBrochureAndVideo;
private String prodBrochureURL;
private String prodVideoURL;
private static int clickedItemId;
ActionBar actionBar;
private static HashMap<String, String> cookieWithRequest = new HashMap<String, String>();

static Object json;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.product_brochure);

    actionBar = getActionBar();
    actionBar.hide();

    Intent intent = getIntent();
    cookie = intent.getStringExtra(BsharpConstant.WEB_SERVICES_COOKIES);
    nid = intent.getStringExtra(BsharpConstant.PRODUCT_NODE_ID);
    clickedItemId = intent.getIntExtra(BsharpConstant.CLICKED_ITEM_ID, 0);
    String jsonResponseFromWebservices = WebserviceBsharpUtil
            .callWebServicesToGetTheProductBrochureAndVideo(cookie, nid);
    urlFromResponse(jsonResponseFromWebservices);

    cookieWithRequest.put(BsharpConstant.WEB_SERVICES_COOKIES, cookie);

    switch (clickedItemId) {
    case 0:
        if (!prodBrochureURL.isEmpty()) {
            try {
                new DownloadFile();
            } catch (ActivityNotFoundException e) {
                Toast.makeText(this,
                        "No Application Available to View PDF",
                        Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(this, "No PDF is Attached with this Product",
                    Toast.LENGTH_SHORT).show();
        }
        break;
    case 1:
        if (!prodVideoURL.isEmpty()) {
            try {
                new DownloadFile();
            } catch (ActivityNotFoundException e) {
                Toast.makeText(this,
                        "No Application Available to View PDF",
                        Toast.LENGTH_SHORT).show();
            }
            break;
        } else {
            Toast.makeText(this, "No Video is Attached with this Product",
                    Toast.LENGTH_SHORT).show();
        }
    }
}

/**
 * GetTheBrochureAndAttachedVideoURL
 * 
 * @param jsonResponse
 */
public void urlFromResponse(String jsonResponse) {
    try {
        json = new JSONTokener(jsonResponse).nextValue();
        if (json instanceof JSONArray) {
            JSONArray jsonArray = (JSONArray) json;
            prodBrochureURL = jsonArray.getJSONObject(0).getString(
                    BsharpConstant.PRODUCT_BROCHURE_URL);
            prodVideoURL = jsonArray.getJSONObject(0).getString(
                    BsharpConstant.PRODUCT_VIDEO_URL);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

}

private class DownloadFile extends AsyncTask<Void, Void, String> {

    @Override
    protected String doInBackground(Void... params) {
        String filename = "brochure.pdf";

        HttpURLConnection connection;
        try {
            URL url = new URL(prodBrochureURL);
            connection = (HttpURLConnection) url.openConnection();
            connection.addRequestProperty(
                    BsharpConstant.WEB_SERVICES_COOKIES, cookie);
            connection.setDoOutput(true);
            connection.connect();
        } catch (IOException e1) {
            return e1.getMessage();
        }

        File folderDir = new File(getExternalFilesDir("Bsharp_PDF")
                + "/Download");

        File file = new File(folderDir, filename);

        if (file.exists()) {
            file.delete();
        }

        if ((folderDir.mkdirs() || folderDir.isDirectory())) {
            try {
                InputStream inputStream = connection.getInputStream();
                FileOutputStream fileOutputStream = new FileOutputStream(
                        folderDir + "/" + filename);

                byte[] buffer = new byte[1024];
                int len1 = 0;
                while ((len1 = inputStream.read(buffer)) != -1) {
                    fileOutputStream.write(buffer, 0, len1);
                }
                fileOutputStream.close();
                inputStream.close();

            } catch (Exception e) {
                e.printStackTrace();
            }

        } else {
            Toast.makeText(getApplicationContext(),
                    "Unable to create folder", Toast.LENGTH_LONG).show();
        }
        return "Done";
    }

    @Override
    protected void onPostExecute(String result) {
        Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG)
                .show();
        super.onPostExecute(result);
    }
}

}

Was it helpful?

Solution

In order for an AsyncTask (DownloadFile in your case) to be executed one has to explicitly call its execute(Params... params) method. In your case in addition to instantiating your task call execute without providing any parameters, i.e.

DownloadFile task = new DownloadFile();
task.execute();

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top