Salman Khan
Salman Khan

Reputation: 2832

Non repeatable request exception in multipartentity android

I am trying to upload an Image File from my app to the server using Multipart approach. As soon as I send the request I am stucking in a trouble called as "Non repeatable request exception". I am new to this approach and have no idea how to deal with this.

Here is my code :-

File imageFile = new File("/mnt/sdcard/link.jpg");
            Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
            byte[] image = stream.toByteArray();
            String img_str = Base64.encodeToString(image, 0);

            InputStream imageStream;
            JSONObject objResult;
            boolean bSucess = true;
            // Base 64 image string was stored with image object , 
            //String imageBase64 = image.getImageString();
            // This base64 to byte , One can directly read bytes from file from Disk
            String imageDataBytes = img_str.substring( img_str.indexOf(",")+1);
            HttpClient client = null;
            HttpPost post = null;
            HttpResponse response = null;
            HttpEntity httpEntity = null;

            imageStream = new ByteArrayInputStream(Base64.decode(imageDataBytes.getBytes(), Base64.DEFAULT));   
            try{
                //Forming Json Object 
                JSONObject jsonImageMetdata = new JSONObject();     
                JSONObject objMultipart = new JSONObject();     
                try {

                    objMultipart.put("status", 1+"");
                    objMultipart.put("type", "Photo");  
                    objMultipart.put("filename", "menu.jpg");
                    objMultipart.put("filetype", "image/jpeg");
                    objMultipart.put("user_id", "1");
                    objMultipart.put("auth_id", "1");
                    objMultipart.put("userfile", Base64.decode(imageDataBytes.getBytes(), Base64.DEFAULT));
                    jsonImageMetdata.put("MultipartImageMetadata", objMultipart);
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                // converting json data to string
                String strImageMetadata = jsonImageMetdata.toString();
                client = new DefaultHttpClient();
                post = new HttpPost("http://stage.phonethics.in/inorbitapp/place_api/menu_list");
                post.setHeader("X-API-KEY", "d41d8cd98f00b204e9800998ecf8427e");
                MultipartEntityBuilder entityBuilder = null;
                try{
                    entityBuilder = MultipartEntityBuilder.create();
                }
                catch(Exception a){
                    Log.d("name",a.getMessage());
                    throw a;                
                }
                entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);    

                // adding text
                entityBuilder.addTextBody("dummyParameter","Dummy text",ContentType.TEXT_PLAIN);             

                // adding Image
                if(imageStream != null){                
                    entityBuilder.addBinaryBody("file", imageStream,ContentType.create("image/jpeg"),"imagename.jpg");
                }         

                // sending formed json data in form of text
                entityBuilder.addTextBody("descriptions", strImageMetadata, ContentType.APPLICATION_JSON) ;
                HttpEntity entity = entityBuilder.build();
                post.setEntity(entity);         
                response = client.execute(post);
                httpEntity = response.getEntity();
                result = EntityUtils.toString(httpEntity);

Can someone help me to sort out this. Any help would be appreciable. Thanks.

Upvotes: 3

Views: 841

Answers (2)

Tudor
Tudor

Reputation: 633

I have solved this problem using a class of JetS3t Amazon toolkit, his name is RepeatableInputStream. In your case import this in your dependencies. In my project I use Ivy. I show you two mode.

Maven:

<dependency>
 <groupId>net.java.dev.jets3t</groupId>
 <artifactId>jets3t</artifactId>
 <version>0.7.2</version>
</dependency>

Ivy:

<dependency org="net.java.dev.jets3t" name="jets3t" rev="0.7.2"/>

After use this class in your imageStream for wrap his as repeatable.

// adding Image
       if(imageStream != null){
         RepeatableInputStream imageStreamRepeatable = new RepeatableInputStream(imageStream,imageDataBytes.getBytes());

         entityBuilder.addBinaryBody("file", imageStreamRepeatable,ContentType.create("image/jpeg"),"imagename.jpg");
       }         

Upvotes: 0

joescii
joescii

Reputation: 6543

My team hit this same issue this week. It turns out that using an InputStream for the body is the root cause of the problem. Since an InputStream is not re-readable, the request is marked as "Non repeatable". You need to use one of the other addBinaryBody method signatures such as the one which accepts a java.io.File.

Upvotes: 2

Related Questions