I created webapp which sends file by FileRepresentation. Client is an Android app. How can I get File from Restlet Response object on the client side?
The file content will be present within the payload. So you can extract it like any payload with Restlet, as described below:
ClientResource cr = new ClientResource(...);
Representation rep = cr.get();
In fact, the FileRepresentation class is provided in order to fill request / response from a file but can't be used to extract content of a response.
To have access to your response content on the client side, it depends on the file type. If you receive an ascii content, you can do something like that:
Representation representation = cr.get();
String fileContent = representation.getText();
If it's a binary file, you need to work with a stream, as described below:
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
cr.get().write(outputStream);
byte[] fileContent = outputStream.toByteArray();
Hope it helps you,
Thierry
Related
In my application I would like to use a gpx file downloaded from the server.
I use GPXParser from https://github.com/ticofab/android-gpx-parser .
When I was trying to parse gpx file, I got warring "java.io.FileNotFoundException: No content provider"
Gpx parsedGpx = null;
GPXParser parser = new GPXParser();
InputStream inputStream = getContentResolver().openInputStream(Uri.parse(stringUrl));
parsedGpx = parser.parse(inputStream);
How can I solve my problem?
getContentResolver is to get data from a ContentResolver not from any random Url, it is for getting data from other processes and usually takes the string form of content://....
You need to download the gpx first with something like HttpURLConnection and it's getInputStream() method to get an inputStream to use with the parser.
I am using Gmail API on Android and I have a problem for file attachment.
I followed example below:
https://developers.google.com/gmail/api/guides/sending#creating_messages_with_attachments
and the problem is arisen here:
String encodedEmail = Base64.encodeBase64URLSafeString(bytes.toByteArray());
I want to attach a video file about 8MB, but this line shows 'out of memory' error.
It is okay when I send image file attachment. (Typically below 1MB).
How can I deal with it?
I don't know android, so I may be way off base, but their code shows:
mimeBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(fileDir + filename);
mimeBodyPart.setDataHandler(new DataHandler(source));
mimeBodyPart.setFileName(filename);
String contentType = Files.probeContentType(FileSystems.getDefault()
.getPath(fileDir, filename));
You are encoding the base64 directly, which explodes into a fairly large string. The FileDataSource likely works around that.
My Android app is requesting through a HTTP POST command a download request. This download is a MultiPart session sending JSONObjects and File (Image).
but when I'm trying to retrieve the File from:
ObjectInputStream objIn = new ObjectInputStream(con.getInputStream());
File p = new File(path, o.photo_id+".png");
p.canWrite();
p = (File) objIn.readObject();
My file p is getting a 0 length...
as I'm not familiar with InpuStream/OutputStream I don't know exactly how to write my File physically in memory....and first to retrieve it !
any idea is more than welcome !
I don't know how to get an InputStream(Read gzipped local xml file) from the locally stored gzip xml file.
employee.gz
If someone Can help I really appreciate. Thanks
This link works with zip. http://techdroid.kbeanie.com/2010/10/unzip-files-in-android.html
I am not sure if it will work with gz files, but you could give it a try. There's a documentation on GZIPInputStream class on the dev docs.
http://developer.android.com/reference/java/util/zip/GZIPInputStream.html
This piece of code works.
GZIPInputStream inputStream = new GZIPInputStream(new FileInputStream(new File(
"path to file")));
String str = IOUtils.convertStreamToString(inputStream);
I have used a util class which converts the input stream to a string. You might want to do the reading part manually.
I'm attempting to use the gdata project from an android app. I'm attempting to upload a new csv file to google docs, but I keep encountering a 411 error (Content-Length).
My code looks something like:
GoogleService ss = new SpreadsheetService("testApp");
ss.setUserCredentials("<username>", "<password>");
DocumentListEntry newEntry = null;
newEntry = new DocumentListEntry();
newEntry.setTitle(new PlainTextConstruct("my.csv"));
TextConstruct tc = TextConstruct.plainText("1,2,3,4,5,6,7,8,9,10");
newEntry.setContent(new TextContent(tc));
DocumentListEntry res = ss.insert(new URL("https://docs.google.com/feeds/default/private/full/"), newEntry);
Since the GData lib is abstracting the network calls from me I assume that I don't need to set the Content-Length myself, which leads to me believe I'm simply not using the lib correctly.
What am I missing? Thanks.
The content of the file should not be set in the metadata but sent along the metadata using an HTTP multipart request.
The client library takes care of doing that and you can set the content using:
newEntry.setFile(/* java.io.File instance */ file, /* MIME type */ "text/csv");
This requires to load the data from a file, but you can use streams to load from memory.
A more detailed example can be found in the Java client library project page.