Wavrik
Wavrik

Reputation: 61

How send key value in addMultipartParts Ion Koush library?

I'm using the Ion Koush Library in my android project, it is working fine but I don't know how to specify the key request when I use the addMultipartParts function.

My code:

Ion.with(this)
   .load("POST", UPLOADIMAGESTG_URL)
   .setTimeout(5000)
   .setMultipartParameter("omni_id", String.valueOf(omniTesteGrade.getOmniProduct().get_id()))
   .addMultipartParts(files)
   .asJsonObject()
   .setCallback(new FutureCallback<JsonObject>() {


    @Override        
    public void onCompleted(Exception e, JsonObject result) {

                if (e != null) {
                    pDialog.setTitleText("Não foi possível enviar imagens, enviando dados do produto");
                    sendTgToServer(json);
                } else {
                    pDialog.setTitleText("Enviando dados do produto");
                    sendTgToServer(json);
                }
            }

   });

It is working fine on the server side because I pick all the inputs with a laravel function Input::All(). But when I want to pick specific request I use this code

   foreach($fileObjects as $fileObject) {
            if (is_file($fileObject)) {
                $imageList[] =$fileObject;
            } else {
                $id = $fileObject;
            }
   }

And this is wrong one more process to server work and I don't want it.

Upvotes: 0

Views: 255

Answers (1)

Abhinav Suman
Abhinav Suman

Reputation: 992

The things are not pretty clear from your questions, that you want to upload a single Image file to the server or Multiple files to the server.

So basically, there is a very simple way you can achieve this thing.

  1. To upload a single file:-

    Ion.with(this)  
    .load("POST", UPLOADIMAGESTG_URL)  
    .uploadProgressBar(uploadProgressBar)   
    .setTimeout(5000)   
    .setMultipartParameter("omni_id", String.valueOf(omniTesteGrade.getOmniProduct().get_id()))  
    .setMultipartFile("key to upload image", new File(files))  
    .asJsonObject()  
    .setCallback(new FutureCallback<JsonObject>() { 
    @Override        
    public void onCompleted(Exception e, JsonObject result) {  
    
                if (e != null) {
                    pDialog.setTitleText("Não foi possível enviar imagens, enviando dados do produto");
                    sendTgToServer(json);
                } else {
                    pDialog.setTitleText("Enviando dados do produto");
                    sendTgToServer(json);
                }
     });  
    
  2. To upload multiple files:-

ArrayList fileParts = new ArrayList<> ();

for (int i = 0; i < salonPhotos.size(); i++) {
   Part part = new FilePart("image_name[" + i + "]", image_value[i]);
   fileParts.add(part);
}


Ion.with(getContext())
   .load("POST", MY_POST_URL)
   .setMultipartParameter("my_text_key", "my_text_value")
   .setMultipartParameter("my_text_key_2", "my_text_value_2")
   .addMultipartParts(fileParts);

Try this, May it help you.

Upvotes: 0

Related Questions