1

I am developing one android application and in my application i want to send image data to my server. So I am using put request. I dont want to send image data with json object. I just want to send image data as body of HTTP put request. And also want to set header content length as well. So I tried it in following ways.

@Override
      protected Void doInBackground(Void... unused) {

         HttpClient hc = new DefaultHttpClient();
            String message;
            HttpPut p = new HttpPut("abc.com");

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bitmap.compress(CompressFormat.JPEG, 100, bos);
            byte[] data = bos.toByteArray();


    try {

              p.setEntity(new StringEntity(data.toString()));
              p.setHeader("Content-Length", Long.toString(data.length));
                //p.setHeader("Content-Length", "0");

                HttpResponse resp = hc.execute(p);
                if (resp != null) 
                {
                    if (resp.getStatusLine().getStatusCode() == 204)
                    {

                    }
                }

                Log.d("Status line", "" + resp.getStatusLine().getStatusCode());
            } catch (Exception e) {
                e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Void... unsued) {

    }

    @Override
    protected void onPostExecute(Void result) {
        try {
            if (dialog.isShowing())
                dialog.dismiss();
            Log.i("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@", "before exception "+result);

        } catch (Exception e) {
            Log.i("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@", "inside catch ");
            Toast.makeText(getApplicationContext(),"Error"+e,
                    Toast.LENGTH_LONG).show();
            Log.e(e.getClass().getName(), e.getMessage(), e);
        }
    }
}

So I have two uncleared questions in my mind. 1) I want to send image data to my server without using multipart and without using json object. Just set it into body of request. How to do that? 2) I want to set content length into header part. How to do that? When I tried to set content length it gives me system error client protocol exception

Need help regarding these two questions. thank you.

nilkash
  • 7,408
  • 32
  • 99
  • 176
  • 1
    Never used HttpPut but what I see is that you convert your imagedata in bos to a String. That will not do. Better change it to base64 then. (increases size by 30%). – greenapps Mar 15 '13 at 10:35
  • @greenapps thank you for help. Actually i don't want to convert image into string. I just want to send image as a raw data(binary data) for my request and put content length.. You have any idea how to do that. – nilkash Mar 15 '13 at 10:51
  • As said I never used HttpPut. But there are many examples on this site where people try the same. Please search for HttpPut on this site and you will get a lot of info. Mostly it's advised to use HttpPost. Further I see that you use setHeader() where there is a addHeader() too. What is the difference? – greenapps Mar 15 '13 at 10:55

1 Answers1

0

As one of the commenter stated above, you don't want to change your byte[] into a String. You will most likely corrupt the image you're trying to send (I tried myself).

You should try using ByteArrayEntity instead (as opposed to StringEntity):

p.setEntity(new ByteArrayEntity(data));
baekacaek
  • 1,527
  • 3
  • 22
  • 45