Skip to content

Instantly share code, notes, and snippets.

@kristopherjohnson
Created July 27, 2012 17:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kristopherjohnson/3189389 to your computer and use it in GitHub Desktop.
Save kristopherjohnson/3189389 to your computer and use it in GitHub Desktop.
Create a JSON POST request for Android
/**
* Create HttpPost request with a JSON string body, expecting a JSON response
*/
private HttpPost createJSONPostRequest(String emailAddress, String data) throws JSONException, UnsupportedEncodingException
{
JSONObject json = new JSONObject();
json.put("protocolVersion", "1.0");
json.put("emailAddress", emailAddress);
json.put("data", data);
String jsonString = json.toString();
HttpPost request = new HttpPost(serviceURLString);
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
request.setEntity(new StringEntity(jsonString));
return request;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment