简体   繁体   中英

Sending multipart form data with image and text in android

I have code to send text and multiple image in one request to server, my case is, I loop through local db to get the data and multiple image so I can use addPart(key, textdata); for the text data and addPart(key, filename, inputstream, "application/octet-stream"); for the image data. But the problem is when I have more than one image in one request I only able to send one of them. Here are my complete code . The main problem happens on line 31 when I have more than one image it only send one of them. I will appreciate any help. Thank you.

The problem I think is might be here

 reqEntity.addPart("myFile", yourImage);

In this your key(myFile) remains the same for all the images. so when your images are more than one, it keeps on overwriting the previous image. So I think, you should attach index with your key(starting from 0,1 and so on), something like this for example

 reqEntity.addPart("myFile_"+i, yourImage);

And also send the image_count to server along with images,so that, it will get to know how many images you are actually sending and by having a simple for loop at the server end,they will be able to get all these images. hope this helps.

Try this way

            String sResponse = "";
            String url = "http://www.api.in/rpcs/uploadfiles/?";
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            MultipartEntity entity = new MultipartEntity();


            options1.inPreferredConfig = Bitmap.Config.ARGB_8888;

            byte[] data1 = null,data2= null,data3= null,data4= null,data5= null;

            if(PreferenceManager.getDefaultSharedPreferences(getBaseContext()).contains("endum_image_0"))
            {       up_image1 = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).getString("endum_image_0", "");
                    bitmap = BitmapFactory.decodeFile(up_image1, options1);
                    bitmap.compress(CompressFormat.JPEG, 100, bos1);
                    data1 = bos1.toByteArray();
            }
            if(PreferenceManager.getDefaultSharedPreferences(getBaseContext()).contains("endum_image_1"))
            {       up_image2 = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).getString("endum_image_1", "");
                    bitmap = BitmapFactory.decodeFile(up_image2, options1);
                    bitmap.compress(CompressFormat.JPEG, 100, bos2);
                    data2 = bos2.toByteArray();
            }

            if(PreferenceManager.getDefaultSharedPreferences(getBaseContext()).contains("endum_image_2"))
            {       up_image3 = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).getString("endum_image_2", "");
                    bitmap = BitmapFactory.decodeFile(up_image3, options1);
                    bitmap.compress(CompressFormat.JPEG, 100, bos3);
                    data3 = bos3.toByteArray();
            }

            if(PreferenceManager.getDefaultSharedPreferences(getBaseContext()).contains("endum_image_3"))
            {       up_image4 = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).getString("endum_image_3", "");
                    bitmap = BitmapFactory.decodeFile(up_image4, options1);
                    bitmap.compress(CompressFormat.JPEG, 100, bos4);
                    data4 = bos4.toByteArray();
            }

            if(PreferenceManager.getDefaultSharedPreferences(getBaseContext()).contains("endum_image_4"))
            {       up_image5 = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).getString("endum_image_4", "");
                    bitmap = BitmapFactory.decodeFile(up_image5, options1);
                    bitmap.compress(CompressFormat.JPEG, 100, bos5);
                    data5 = bos5.toByteArray();
            }




            entity.addPart("post_id", new StringBody(post_id));
            entity.addPart("user_id", new StringBody(user_id));
            entity.addPart("cat_id", new StringBody(category));

            if(data1!=null){
            entity.addPart("files[]", new ByteArrayBody(data1,"image/jpeg", "u1.jpg"));
            }
            if(data2!=null){
            entity.addPart("files[]", new ByteArrayBody(data2,"image/jpeg", "u2.jpg"));
            }
            if(data3!=null){
            entity.addPart("files[]", new ByteArrayBody(data3,"image/jpeg", "u3.jpg"));
            }
            if(data4!=null){
            entity.addPart("files[]", new ByteArrayBody(data4,"image/jpeg", "u4.jpg"));
            }
            if(data5!=null){
            entity.addPart("files[]", new ByteArrayBody(data5,"image/jpeg", "u5.jpg"));
            }



            httpPost.setEntity(entity);
            HttpResponse response = httpClient.execute(httpPost);

            BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));

           StringBuilder s = new StringBuilder();

            while ((sResponse = reader.readLine()) != null) 
            {
                s = s.append(sResponse);
            }

            if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
            {
                return s.toString();
            }else
            {
                return "{\"status\":\"false\",\"message\":\"Some error occurred\"}";
            }   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM