0

I need to use the Content-Type", "multipart/mixed and send the Json Body as Content-Type: application/json; charset=UTF-8 and also a file by a Request param "file" by "Content-Type: text/xml", can any body explain how it can be done?

Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43

2 Answers2

1

Yes it is possible to send both, i hope you may have sent ever image as file to server.

0

For file upload/download, I always rely on ION, an Android Asynchronous Networking library. You can refer to one of the sample provided by developer to achieve your goal.

I have modified the sample for your reference, but the settings may differ according to your backend configuration.

JSONObject body = new JSONObject();
body.put("Your key", "Your value");

Ion.with(getContext())
.load("https://koush.clockworkmod.com/test/echo")
.setMultipartParameter("body", body.toString()) // this your jsonBody
.setMultipartFile("file", "application/zip", new File(your file path))
.asJsonObject() // if you want to receive response in json, use this.
.setCallback(...)
Amad Yus
  • 2,856
  • 1
  • 24
  • 32
  • thanks for the reply, but .setMultipartFile("file", "application/zip", new File("/sdcard/filename.zip")) is not accepted when .setJsonObjectBody(body) is added – Rajesh Uragonda Feb 28 '17 at 09:27
  • You can try this option. Instead using setJsonObjectBody, you can use setMultiPartParameter. ... .setMultipartParameter("body", body.toString()) .setMultipartFile("file", "application/zip", new File("/sdcard/filename.zip")). Check the modified answer – Amad Yus Feb 28 '17 at 09:30
  • thank you, but here i do not have a any "key" to pass it as a string. I need to pass the json as a request body. Once please have a look at this link http://stackoverflow.com/questions/29975099/how-resttemplate-supports-for-multipart-mixed-in-consumption you may get idea about my requirement. – Rajesh Uragonda Feb 28 '17 at 09:37
  • I am quite confuse with your requirement. If you want to sent Content-Type, it is not necessarily in JSON. You also can use setMultipartParameter("Content-Type", "application/json") or you can put it in header, like this .addHeader("Content-Type", "application/json"). You can refer this here: http://stackoverflow.com/questions/25153340/android-koush-ion-lib-unable-to-post-and-receive-json-response – Amad Yus Feb 28 '17 at 09:57