0

I have a restful web service, and i need to be able to send an image to it from Android via HttpUrlConnection.

Currently I am converting the image to a string as follows

ByteArrayOutputStream baos=new  ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,100, baos);
byte [] b=baos.toByteArray();
String temp=Base64.encodeToString(b, Base64.DEFAULT);

And sending it in a request like so (Async obviously)

 url = new URL("blabla/users/1/updatePhoto/" + temp);

 connection = (HttpURLConnection) url.openConnection();
 connection.setRequestMethod("POST");
 connection.connect();

This is the error I get. I'm assuming the url is just too long to work, so I'm wondering if theres a better way to do this. Thanks

java.io.IOException: unexpected end of stream on Connection{192.168.0.22:8080, proxy=DIRECT@hostAddress=192.168.0.22 cipherSuite=none protocol=http/1.1} (recycle count=0)

R. Zagórski
  • 20,020
  • 5
  • 65
  • 90
superwelling
  • 109
  • 1
  • 10
  • I do not understand why the image needs to be passed in url instead of post body – S.W. Jan 24 '17 at 02:02
  • Could you point me in the right direction of how this can be done? And in my web service how can I consume both the user id and the image? – superwelling Jan 24 '17 at 12:37
  • Simply put user id in post body, and for image using output stream – S.W. Jan 24 '17 at 14:59
  • @S.W. I think my Android code is working now anyway, I'm sending a request using output stream `OutputStream output = c.getOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 50, output);` How do I consume the request from my server? I'd know how to normally, but I'm not sure where to get the part that was sent with the output stream? (I'm using resteasy) – superwelling Jan 24 '17 at 16:24
  • you may try follow this guide http://stackoverflow.com/questions/25398200/uploading-file-in-php-server-from-android-device – S.W. Jan 25 '17 at 01:48

0 Answers0