Reputation: 171
I have to download a pdf file through a GET request that has the following structure:
http://example.example.com/folder/folder/orders/id/folder/download
In postman the response that gives me the server when making the request is as follows:
BODY:
%PDF-1.4
%����
4 0 obj
<undefined</Type/XObject/ColorSpace/DeviceRGB/Subtype/Image/BitsPerComponent 8/Width 174/Length 4410/Height 65/Filter/DCTDecode>>stream
����JFIF��C
And so on....
HEADERS:
Connection →Keep-Alive
Content-Type →application/pdf
Date →Sun, 16 Feb 2014 04:52:34 GMT
Keep-Alive →timeout=5, max=15
Server →Apache
cache-control →must-revalidate, no-cache, no-store, post-check=0, pre-check=0, private
content-disposition →filename="EXAMPLE.pdf"
content-length →9869
expires →Sat, 25 Mar 1978 10:00:00 GMT
pragma →no-cache
First I tried to capture the body response with:
HttpEntity httpEntity = response.getEntity();
inputStream = httpEntity.getContent();
Then I tried to create a File with pdf extension so then I can call it with an intent and finally show it via an pdf reader app.
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);
Did not work... : ( XD
I saw some example where they talk about downloading files with php and using the "Content-Disposition" Header. but a don't get it. I need to do it on android, please some advice : )
THANKS!
Upvotes: 0
Views: 12889
Reputation: 171
Finally this is the way that work for me : )
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;
}
Upvotes: 2