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.