0

I have a link https://www.pivotaltracker.com/file_attachments/47755990/download to download a docx file. When i hit this URL on browser file downloaded properly for me but when i am trying to download using Android code it does not work for me. My code is given below

public JSONObject postRequest(String url) {
    OutputStream output;
    InputStream inputStream = null;
    String result = "";
    JSONObject response = null ;
    File file = null;
    HttpResponse httpResponse;
    try {
        String directoryPath = "SirConrad";
        File newFolder = new File(Environment.getExternalStorageDirectory(), directoryPath);
        if (!newFolder.exists()) {
            newFolder.mkdir();
        }
        try {

             file = new File(newFolder, "sachsd.docx");
            file.createNewFile();
        } catch (Exception ex) {
            System.out.println("ex: " + ex);
        }
        HttpClient httpclient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);
                httpGet.addHeader("accessToken","29c1322d3072d8fadbf2478c61fb6ff6");
               httpGet.addHeader("content_type","application/vnd.openxmlformats-officedocument.wordprocessingml.document");
                //httpGet.addHeader("UserID", "4");

            httpResponse = httpclient.execute(httpGet);


        // 9. receive response as inputStream
        inputStream = httpResponse.getEntity().getContent();

        output = new FileOutputStream(file);

        byte data[] = new byte[4096];
        long total = 0;
        int count;
        while ((count = inputStream.read(data)) != -1) {
            output.write(data, 0, count);
        }
    } catch (Exception e) {
        Log.d("InputStream", e.getLocalizedMessage());
    }

    // 11. return result
    return response;
}

P.S Link http://pp.sirconrad.com/img/EDDYAdminPanelDesignBreakdown.docx work for me to download attachment in above code but.

Note* i am using Pivotal Traker apis to download this file.

Corby Page
  • 1,385
  • 10
  • 17
Sirconrad
  • 69
  • 1
  • 3
  • 7
  • 'When i hit this URL on browser file downloaded properly'. Not when i click it. I first have to login. Moreover that url does not specify a .docx document. You PS link works for me in browser too. – greenapps Jul 01 '15 at 08:10
  • 'Note* i am using Pivotal Traker apis to download this file.' Wouldn't it me nice if you had started your post with mentioning that? – greenapps Jul 01 '15 at 08:13
  • @greenapps Thanks mate. I will back to you soon :) – Sirconrad Jul 01 '15 at 09:48

0 Answers0