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.