我有一个链接https://www.pivotaltracker.com/file_attachments/47755990/download来下载docx文件。当我在为我正确下载的浏览器文件上点击此URL但是当我尝试使用Android代码下载时,它对我不起作用。我的代码如下:
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让我在上面的代码中下载附件,但是。
注意*我正在使用Pivotal Traker apis下载此文件。