Dealing with multipart responses - android

I'm playing with Amazon Alexa (AVS) and the service mostly sends multipart responses.
For example a response can contain an application/json part associated with an application/octet-stream (MP3 data) part.
For now I don't have any idea the way to write my Retrofit2 service method for it to handle it correctly.
I guess an AlexaMultiPartResponse object here won't work:
#Multipart
#Post("/path")
Call<AlexaMultiPartResponse> getAnswer(#Part("metadata") RequestBody metadata, #Part("audio") RequestBody audio);
Do you have any idea?
Regards.

According to this: https://github.com/square/retrofit/issues/2164 there is no elegant way to do it with Retrofit2.
So I ended up parsing the multipart response with Apache FileUpload MultipartStream (https://commons.apache.org/proper/commons-fileupload/apidocs/org/apache/commons/fileupload/MultipartStream.html)

Related

HTTP Requests Query: Can i send a video over a HTTP request? And Send back json data and images?

So I want to build an android application, that users can take videos on, then send them to a web server to perform analysis on the video, send back metrics in JSON and also send back several images from the video. Is this possible? Is it a good/bad idea? If this is a dumb approach or this will be a very slow approach, any better strategies are welcome. Cheers!
Yes you can send video file to web-server and get response via JSON with images.
For sending video you can HTTP client retrofit which is very handy. You must use #Multipart in your API calling. Here is an example.
#POST("/your_url/")
Call<ResultObject> uploadVideoToServer(#Part MultipartBody.Part video);
You can call it like:
File videoFile = new File(pathToVideoFile);
RequestBody videoBody = RequestBody.create(MediaType.parse("video/*"), videoFile);
MultipartBody.Part vFile = MultipartBody.Part.createFormData("video", videoFile.getName(), videoBody);
Your need a dedicated server (aws or digital ocean droplet) and well formed web apps in server (algorithm) to extract picture from your video for better user experience. Overall your idea is good & it's possible.

Converting postman request to retrofit 2

I have some http request related problem which I am not able to solve.
I have aws url on which I have to make PUT request with mp4 video file to upload with header "Content-Type=video/mp4"
I wrote the request in postman and successfully uploaded the mo4 video file. Here is my postman screenshot.
I tried implementing it in Android with retrofit and successfully did it as well with 200 response from server. But the problem is, video file is not being uploaded in correct format and getting error in aws console.
Get this error: 4000 3f0fbcf1-1f39-4dc8-9d1e-332966662dd0: Amazon
Elastic Transcoder could not interpret the media file.
Here is my retrofit implementation
#PUT
#Multipart
fun postVideo(#Header("Content-Type") contentType: String
, #Url url: String
, #Part video: MultipartBody.Part): Call<ResponseBody>
And posting body as following
val requestBody = RequestBody.create(MediaType.parse("video/mp4"),videoFile)
val body = MultipartBody.Builder().addPart(requestBody).build()
As far as I know, I think my problem lies in the way I am attaching body to my request and header and url I am giving properly (as I am getting 200ok from server)
My lack of HTTP knowledge is giving me nightmares, any help is appreciated.

How to POST multiple large image files via Volley Android

I am trying to post around 10 images via volley by converting it to base64 but Volley post the images multiple time due to its retry policy. I am already compressing the files and i also tried changing its timeout but problem still exist. Retrofit also doing same.
Why do you need/use base64?
Are you using get rather than post?
Use multipart file upload for large files, for instance (with retrofit):
public interface FileUploadService {
#Multipart
#POST("/upload")
void upload(#Part("myfile") TypedFile file,
#Part("description") String description,
Callback<String> cb);
}
You can use Android Asynchronous Http Client for multipart request.
Refer this link
HttpClient support Base64.

WCF Multipart Custom Class

I send a POST request from an Android App using the ION library. The request has some multipart parameters and an attachment file.
Something like this:
.setMultipartParameter("Message", message)
.setMultipartParameter("TextFileName", textFileName)
.setMultipartParameter("TextFileContentBase64Encoded", TextFileContentBase64Encoded)
.setMultipartFile("AttachementFile", fileName)
The problem is that server side(WCF), we have a custom class used for parsing this request that has a Stream for the attachment file, and 3 Strings for other parameters. From my understanding, WCF limits you. You can't parse a custom class that has a Stream and other parameters. You either have a Stream or you have other params.
The question is how can you parse a incoming request that has a stream and other parameters in WCF?
Thanks!

Post With token_id and Json

Forgive me as I'm new to API driven development. I'm doing a front-end and back-end at the same time and ran into a snag.
On the back-end I accept URL encoded token_id that you may post to create a sessions. Goes like
http://someip:3000/sessions/create?token_id=sometoken
and works.
But now to create a post object the back-end expects a JSON object and a token_id. But reading some documentation for my front-end (android+retrofit) I understand that I can't URL encode a field (my token_id) and send a body as JSON.
Maybe I'm taking the wrong approach. Which path should I take to receive the token_id and a json object at the same time on the back-end?
I understand that I can't URL encode a field (my token_id) and send a
body as JSON.
This is true if you are trying to send POST parameters, because those are sent as the body. But you appear to be using query parameters which are part of the URL. You can use query parameters and JSON body in the same request. Your interface would be something like following, adjusting the body and return types for your particular case.
#POST("/sessions/create")
Call<Response> create(#Query("token_id") String tokenID, #Body MyBodyClass body);

Categories

Resources