I am trying to send MultiPartEntity to the server to upload an image.
I have to send data to the server with a MultiPartEntity, The following is a sample request.
{
"user": {
"id": "12345",
"primary_account_id": "43566"
},
"poster_photo": {
"title": "photo test",
"image": *uploaded image
}
}
Does anyone have an idea on how to do this?
Please help me solve this.
There are two way to achieve your goal.
1)Using JSON
Android Side:- First thing is JSON format can support only string data, so you can add only string data. If you want to add image in JSON format so you have to convert Image to String. Image into bytearray
Server Side:- Decode byte string and convert in Image.
2)Using NameValuePair
Android Side:- Used NameValuePair instead of JSON, for this way follow link which HardikJoshi given in comment (Link)
Server Side:- Change server code JSON to NameValuePair.
I suggest Second Way is easy to implement & understand..
Related
I am trying to retrieve metadata of a video file present in my google drive. I can access all the information related to that file (like size, mimetype, etc.). But, when I am trying to retrieve metadata of that video file I am getting a null value. Could you please tell me how can I retrieve metadata of a video file. Thanks in advance.
VideoMediaMetadata video_Media_MetaData=file.getVideoMediaMetadata();
AFAIK, a successful HTTP request using Files: list will return a response body which includes videoMediaMetadata in the following structure:
{
"kind": "drive#fileList",
"nextPageToken": string,
"incompleteSearch": boolean,
"files": [
"videoMediaMetadata": {
"width": integer,
"height": integer,
"durationMillis": long
},
"isAppAuthorized": boolean
]
}
However, please note that, as described in Files documentation, videoMediaMetadata is
Additional metadata about video media. This may not be available immediately upon upload.
Thus, it will return null for none as mentioned here.
With this, you may want to try Files: update to update a file's metadata and/or content with patch semantics.
I need to develop a program where I have to calculate driving distance and time. Getting the request to the Google Maps API works, and I get a really big response. Now I want to extract the data I need from this response. The response is in json, and the program I make is made in Java. I have no idea to do this. I tried to just put the whole response in a string, and then search the data I need, like "distance" and extract a few characters that come after that. Problem with this is, if something changes in front of it, it's all going to be at different indexes in the huge string.
A response is like this (only a bit of the big thing)
"copyrights" : "Kaartgegevens ©2017 GeoBasis-DE/BKG (©2009), Google",
"legs" : [
{
"distance" : {
"text" : "229 km",
"value" : 229411
},
"duration" : {
"text" : "2 uur 20 min.",
"value" : 8417
},
How do I easily extract the value of both "distance" and "duration"?
Basically what you want to do is serializing and deserializing the JSON to a proper Java object. You can do this by using Frameworks like Jackson or GSON to convert the JSON-String to POJOs.
This might help you as well:
How to serialize and deserialize a JSON object from Google geocode using Java
Converting JSON to Java
http://thegeekyland.blogspot.de/2015/11/serializing-and-deserializing-json-from.html
And I'm pretty sure Google has it's own API that maps the JSON internally, although I haven't used it yet. Here you might find more information:
https://developers.google.com/maps/web-services/?hl=de
Hope this helps!
I am trying to add the source URL on an existing resource but I can't get it to work.
This is what I have:
HttpEntity reqEntity = MultipartEntityBuilder.create()
.addPart("id", new StringBody("resource id",ContentType.TEXT_PLAIN))
.addPart("Source", new StringBody("www.google.com",ContentType.TEXT_PLAIN))
.build();
postRequest = new HttpPost(host+"/api/3/action/resource_update");
postRequest.setEntity(reqEntity);
postRequest.setHeader("X-CKAN-API-Key", "myApi");
Which gives me the following error:
"success": false, "error": {"__type": "Validation Error", "url": ["Missing value"]}
I can add a Source field when I upload a file, but it doesn't create a link for it. Maybe because I use ContentType.TEXT_PLAIN instead of something that will tell CKAN that this is a link?
Edit:
By source I mean the link on the resource information where it points to the webpage of the original data. When manually uploading a file you have that option, I just cant get it to work with the API.
The error is telling you that you've not specified the url value for the resource, which is necessary.
The content that you POST needs to be in JSON format, whereas you appear to be sending it as multipart form data.
The JSON keys you need to send for a resource are id, url, name, format etc. i.e. you need to set url rather than Source. You can see the existing keys by looking at /api/action/resource_show?id=xyz.
The normal way to use resource_update is to do resource_show first, edit the JSON with the new values, and then resource_update. That way you don't lose any other properties of the resource, such as format.
I'm sending a file over the network which is in JSON format and wanting to retrieve information from it. The file is created using the File.createTempFile method. Here's the code:
File patchFile = File.createTempFile("indexer", ".pf", null);
try(FileOutputStream fos = new FileOutputStream(patchFile)) {
byte[] patchFileBytes = new byte[payloadLength];
buffer.readBytes(patchFileBytes);
fos.write(patchFileBytes);
}
I'm trying to find the best way to read the patchFile as JSON and get values from it. The generated files structure looks like this(on the client):
{
"0": [
{
"fileId": "Cache.dat",
"fileChecksum": "d41d8cd98f00b204e9800998ecf8427e"
},
{
"fileId": "Character.fbx",
"fileChecksum": "d41d8cd98f00b204e9800998ecf8427e"
},
{
"fileId": "Skybox.png",
"fileChecksum": "d41d8cd98f00b204e9800998ecf8427e"
}
],
"1": [
{
"fileId": "indexer.pf",
"fileChecksum": "f8130e38ce5c58ccbf200d24c2629632"
}
]
}
Currently I've got the file being sent over and read into memory as you can see above, but I'm unsure as to where to go from here on actually reading the data, could anyone help me out?
I'm using GSON.
What I would suggest is to avoid writing to a temporary file, instead wrap the buffer with a reader and use in one of the API's
(I'm assuming that the buffer is an InputStream, then use java.io.InputStreamReader to convert it into a Reader)
GSON has a lot of API's to perform your required task:
Examples:
To follow the iterative approach, then look at com.google.gson.JsonParser#parse it accepts reader or string to get elements.
If you know what is the type of the element being deserialized, then you can look at using com.google.gson.Gson#fromJson methods.
See the following API docs, it might be helpful:
JsonStreamParser
JsonParser
Gson
You did not mention how big your file can be. If it is not too big then you could just use Apache Commons IO and IOUtils.toString and read the entire file content into String. And since you already know GSON it should be pretty simple from there.
I would be obliged, if you could kindly let me know the means by which I can retrieve the youtube thumbnails using Google API Client Library for Java, similar to the way in which we fetched thumbnails using gdata.
List<String> thumbnails = new LinkedList<String>();
for (MediaThumbnail mediaThumbnail : mediaGroup.getThumbnails()) {
thumbnails.add(mediaThumbnail.getUrl());
}
Looking forward,
Regards,
Rony
Using the example URL from the reference page:
URL: http://gdata.youtube.com/feeds/api/videos?q=football+-soccer&orderby=published&start-index=11&max-results=10&v=2&alt=jsonc
we can extract an example of one of the relevant pieces of JSON:
"thumbnail": {
"sqDefault":"http://i.ytimg.com/vi/PpUgUrU5XUA/default.jpg",
"hqDefault":"http://i.ytimg.com/vi/PpUgUrU5XUA/hqdefault.jpg"
}
You can modify the sample classes to parse the thumbnail object.