使用“通过REST服务下载PDF文件”;“内容处置”;Android上的标题
我必须通过具有以下结构的GET请求下载pdf文件:使用“通过REST服务下载PDF文件”;“内容处置”;Android上的标题,android,rest,pdf,get,download,Android,Rest,Pdf,Get,Download,我必须通过具有以下结构的GET请求下载pdf文件: http://example.example.com/folder/folder/orders/id/folder/download 在postman中,发出请求时,服务器给出的响应如下: 正文: 首先,我尝试用以下方法捕捉身体反应: HttpEntity httpEntity = response.getEntity(); inputStream = httpEntity.getContent(); 然后我尝试创建一个扩展名为pdf的文件
http://example.example.com/folder/folder/orders/id/folder/download
在postman中,发出请求时,服务器给出的响应如下:
正文:
首先,我尝试用以下方法捕捉身体反应:
HttpEntity httpEntity = response.getEntity();
inputStream = httpEntity.getContent();
然后我尝试创建一个扩展名为pdf的文件,这样我就可以有目的地调用它,并最终通过pdf阅读器应用程序显示它
String extStorageDirectory = Environment.getExternalStorageDirectory()
.toString();
File folder = new File(extStorageDirectory, "PDF");
folder.mkdir();
File file = new File(folder, "example.pdf");
try {
file.createNewFile();
FileUtils.writeStringToFile(file, inputStream);
} catch (IOException e1) {
e1.printStackTrace();
}
File fileobject_in_other_method = new File(Environment.getExternalStorageDirectory()+"/PDF/example.pdf");
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/pdf");
startActivity(intent);
没用…:(XD)
我看到一些例子,其中他们谈到用php下载文件并使用“内容处置”标题。但a不明白。我需要在android上完成,请给出一些建议:)
谢谢 最后,这就是我的工作方式:)
你找到解决方案了吗?是的,请原谅我的耽搁,我会发布答案:)
HttpEntity httpEntity = response.getEntity();
inputStream = httpEntity.getContent();
String extStorageDirectory = Environment.getExternalStorageDirectory()
.toString();
File folder = new File(extStorageDirectory, "PDF");
folder.mkdir();
File file = new File(folder, "example.pdf");
try {
file.createNewFile();
FileUtils.writeStringToFile(file, inputStream);
} catch (IOException e1) {
e1.printStackTrace();
}
File fileobject_in_other_method = new File(Environment.getExternalStorageDirectory()+"/PDF/example.pdf");
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/pdf");
startActivity(intent);
public String downloadPdf() {
String responseServer = "";
try {
//////PDF folder and file init///////////////////////////
HttpClient client = HttpClientCustomized.getHttpsClient(new DefaultHttpClient());
//you can use the default Http Client class instead
HttpGet get = new HttpGet(sUrl);
get.addHeader("x-wsse", header);
HttpResponse response = client.execute(get);
responseCode = response.getStatusLine().getStatusCode();
Log.d("responseCode", responseCode + "");
HttpEntity httpEntity = response.getEntity();
is = httpEntity.getContent();
try {
String typeS = "someFileName".pdf";
directory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/SomeFolderName/");
// if no directory exists, create new directory
if (!directory.exists()) {
directory.mkdir();
}
FileOutputStream fi = new FileOutputStream(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/ShippingOrders/" + typeS);
byte[] buf = new byte[8 * 1024];
int len = 0;
while ((len = is.read(buf)) > 0) {
fi.write(buf, 0, len);
}
fi.close();
is.close();
System.out.println("Hurra!! : D");
} catch (IOException e) {
System.out.println("owww : (" + e.toString());
}
responseServer = "TRUE";
} catch (IOException e) {
Log.d("IOException", e.toString());
return "Problems with the server";
}
return responseServer;
}