Post with image attachment

All your problems with Audio, Video and Images.

Post with image attachment

Postby ugurbyrm » Sat Jan 22, 2011 3:18 pm

hi everyone. im new in android dev and stuck on posting an image to jersey web service.
here is code snipets of android app and web service.

Android app
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                         HttpClient httpClint = new DefaultHttpClient();
  2.                         HttpPost httpPost = new HttpPost(BASE_URI);                    
  3.                         MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
  4.                          
  5.                         Bitmap bmImg = BitmapFactory.decodeFile("sdcard/22_6185.jpg");
  6.                         ByteArrayOutputStream bos = new ByteArrayOutputStream();
  7.                         bmImg.compress(CompressFormat.JPEG, 100, bos);
  8.                         byte[] bitmapdata = bos.toByteArray();
  9.                        
  10.                         entity.addPart("Default",new StringBody("Default", Charset.forName("UTF-8")));
  11.                         entity.addPart("6",new StringBody("6",Charset.forName("UTF-8")));
  12.                         entity.addPart("100", new StringBody("100",Charset.forName("UTF-8")));
  13.                         entity.addPart("jpg",new InputStreamBody(new ByteArrayInputStream(byteData),"jpeg"));
  14.                                 httpPost.setEntity(entity);            
  15.                         HttpResponse httpResponse = httpClint.execute(httpPost);
  16.                        
  17.                                
Parsed in 0.034 seconds, using GeSHi 1.0.8.4



Web service


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. try {
  2.             newSearchOption = getStringFromInputStream(((BodyPartEntity) multipart.getBodyParts().get(0).getEntity()).getInputStream());
  3.             newCategoryName = getStringFromInputStream(((BodyPartEntity) multipart.getBodyParts().get(1).getEntity()).getInputStream());
  4.             newCategoryId = Integer.parseInt(newCategoryName);
  5.             numberOfResult = getIntegerFromInputStream(((BodyPartEntity) multipart.getBodyParts().get(2).getEntity()).getInputStream());
  6.             BodyPartEntity bpe = (BodyPartEntity) multipart.getBodyParts().get(3).getEntity();
  7.             InputStream is = bpe.getInputStream();
  8.             bufferedImage = ImageIO.read(is);
  9.         } catch (IOException e) {
  10.             e.printStackTrace();
  11.         }
  12.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4



and here is the error message a receive everytime :

HTTP Status 415 - Status report
message
description
The server refused this request because the request entity is in a format not supported by the requested resource for the requested method ().
GlassFish Server Open Source Edition 3.0.1

it seems there is a problem on entity parts but couldnt figure out.
i tried afew more code snipets on this forum eigther but nothing changed.

thanks in a advance
ugurbyrm
Freshman
Freshman
 
Posts: 2
Joined: Sat Jan 22, 2011 12:26 pm

Top

Re: Post with image attachment

Postby ugurbyrm » Tue Jan 25, 2011 4:45 am

After searching alot we discover that InputStreamBody doesnt work properly for our system. after posting the image to web service, content lentgh seems as -1on server site. so we wrote an AbstarctContentBody as below.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import java.io.ByteArrayInputStream;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.OutputStream;
  5. import java.util.Collections;
  6. import java.util.Map;
  7.  
  8. import org.apache.http.entity.mime.MIME;
  9. import org.apache.http.entity.mime.content.AbstractContentBody;
  10. import org.apache.http.entity.mime.content.ContentBody;
  11.  
  12. public class ByteBody extends AbstractContentBody implements ContentBody {
  13.  
  14.     private final byte[] mContent;
  15.  
  16.     public ByteBody(final byte[] content) {
  17.         super("application/octet-stream");
  18.         if (content == null) {
  19.             throw new IllegalArgumentException("File may not be null");
  20.         }
  21.         this.mContent = content;
  22.     }
  23.  
  24.     public InputStream getInputStream() throws IOException {
  25.         return(new ByteArrayInputStream(mContent));
  26.     }
  27.  
  28.     public String getTransferEncoding() {
  29.         return MIME.ENC_BINARY;
  30.     }
  31.  
  32.     public String getCharset() {
  33.         return null;
  34.     }
  35.  
  36.     public String getMimeType() {
  37.         return "application/octet-stream";
  38.     }
  39.  
  40.     public Map<String, String> getContentTypeParameters() {
  41.         return Collections.emptyMap();
  42.     }
  43.  
  44.     public String getMediaType() {
  45.         return "application";
  46.     }
  47.  
  48.     public String getSubType() {
  49.         return "octet-stream";
  50.     }
  51.  
  52.     public long getContentLength() {
  53.         return this.mContent.length;
  54.     }
  55.  
  56.     public String getFilename() {
  57.         return("bytearray");
  58.     }
  59.  
  60.         @Override
  61.         public void writeTo(OutputStream out) throws IOException {
  62.                 if (out == null) {
  63.             throw new IllegalArgumentException("Output stream may not be null");
  64.         }
  65.         out.write(mContent, 0, mContent.length);
  66.                
  67.         }
  68. }
  69.  
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


and image is safely posted to server by using code as below.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.           entity.addPart("default", new StringBody("Default"));
  2.           entity.addPart("catid", new StringBody(String.valueOf(catId)));
  3.           entity.addPart("result", new StringBody("25"));
  4.           entity.addPart("image", new ByteBody(bitmapdata));
  5.           httpResponse = httpClint.execute(httpPost);
  6.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
ugurbyrm
Freshman
Freshman
 
Posts: 2
Joined: Sat Jan 22, 2011 12:26 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 12 guests