Reputation: 3078
I have to work on an application where i need to send the class object as Parameter using Multipart Entity along with a file. I have checked that the Multipart addpart method doesn't accept an Object to post to Web Api.Below is the Code i have tried. Any Suggestions on how to pass the object would be great.
HttpPost post = BaseActivity.getHttpPost("MyURL");
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE,"",Charset.defaultCharset());
try {
ServiceJobFileModel sjfm = new ServiceJobFileModel();
sjfm.setFileExtension(myfileextension);
sjfm.setCapturedDate(captdate);
sjfm.setFileName("/pathtofile/filename.extension");
File f = new File(sjmf.getFileName());
entity.addPart("file", new FileBody(f));
entity.addPart("serviceJobFileModel", sjfm); // Compiler Error
post.addHeader("Accept-Encoding", "gzip, deflate");
post.setEntity(entity);
} catch (UnsupportedEncodingException e) {
Log.v("encoding exception","E::: "+e);
e.printStackTrace();
}
The Web Api is developed in Dotnet which is why the windows mobile app has sent the parameters as
var parameters = new ServiceJobFileModel()
{
FileExtension = serviceJobFileModel.FileExtension,
CapturedDate = serviceJobFileModel.CapturedDate,
ServiceJobNumber = serviceJobFileModel.ServiceJobNumber
};
content.Add(new StreamContent(filestream), "file", Path.GetFileName(fileName));
content.Add(new ObjectContent<ServiceJobFileModel>(parameters, new JsonMediaTypeFormatter()), "serviceJobFileModel");
Upvotes: 2
Views: 358
Reputation: 13396
IMHO you should serialize your object in a text format like JSON or XML and then use a StringBody
to send it.
Upvotes: 2