You can use this to post an Entity to server:
HttpClient httpClient = new DefaultHttpClient(); HttpPost postRequest = new HttpPost(url); postRequest.setEntity(entity); try { HttpResponse response = httpClient.execute(postRequest ); String jsonString = EntityUtils.toString(response .getEntity()); Log.v(ProgramConstants.TAG, "after uploading file " + jsonString); return jsonString; } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
An Entity can be name value pair:
List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("key1", value1)); nvps.add(new BasicNameValuePair("key2", value2)); Entity entity=new UrlEncodedFormEntity(nvps, HTTP.UTF_8)
Or you can send an entity with bytearray.
Bitmap bitmapOrg=getBitmapResource(); ByteArrayOutputStream bao = new ByteArrayOutputStream(); bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao); byte[] data = bao.toByteArray(); MultipartEntity entity=new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE) entity.addPart("file", new ByteArrayBody(data, "image/jpeg", "file"));
If you want to post json to server:
Please check out this link How do I send JSon as BODY In a POST request to server from an Android application?
For serializing and deserializing java object, I recommend https://sites.google.com/site/gson/gson-user-guide#TOC-Using-Gson Really hope it can help you see an overview of sending data to server