2

This is the first time ever I am using multi part request to upload data on the server.I am using ion for service hit can you please let me know how can I post my data on the server? This is my request parameter

jSON Request:
 {s_id 
note_name
 file_name 
tag_name 
set_date 
mark_as_done
 clear_reminder
 Description 
media_name1 
media_name2
 media_name3
 Count = 3 }

Along with it, I have to upload file media1, media2, media2 on same api**. Any help would be greatly appreciated.Thanks

bhumika rijiya
  • 440
  • 1
  • 5
  • 42
Tulika Kansal
  • 57
  • 1
  • 8

1 Answers1

0

You can't do a JSON body and a Multipart body in one HTTP request.

you can send your json as a parameter :)

// first create your json object
    JsonObject object = new JsonObject();
    object.addProperty("note_name", "your value goes here");
    object.addProperty("description", "your description goes here");
    // add other stuffs here

    //create list of files to upload
    List<Part> files = new ArrayList<>();
    for (int i = 1; i <= 3; i++) {
        files.add(new FilePart("file" + i, new File("path of file like: storage/image/....")));
    }

    Ion.with(context)
            .load("POST","http://example.com")
            .uploadProgress(new ProgressCallback() {
                @Override
                public void onProgress(long downloaded, long total) {
                    int percent = (int) (downloaded * 100 / total);
                    // update your progressbar with this percent if needed
                }
            })
            .addMultipartParts(files)
            .setMultipartParameter("json", object.toString()) // your json is here
            .asString()
            .setCallback(new FutureCallback<String>() {
                @Override
                public void onCompleted(Exception e, String result) {
                    if (e != null) {
                        // error: log the message here
                        return;
                    }
                    if (result != null) {
                        // result is the response of your server
                    }
                }
            });

Hope this help you :)

Ali Sherafat
  • 3,506
  • 2
  • 38
  • 51