Skip to content

Instantly share code, notes, and snippets.

@danpariente
Last active December 25, 2015 01:19
Show Gist options
  • Save danpariente/6894160 to your computer and use it in GitHub Desktop.
Save danpariente/6894160 to your computer and use it in GitHub Desktop.
@Override
protected JSONObject doInBackground(String... urls) {
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(urls[0]);
JSONObject holder = new JSONObject();
JSONObject userObj = new JSONObject();
String response = null;
JSONObject json = new JSONObject();
try {
try {
// setup the returned values in case
// something goes wrong
json.put("success", false);
json.put("info", "Something went wrong. Retry!");
// add the user email and password to
// the params
userObj.put("email", mUserEmail);
userObj.put("password", mUserPassword);
holder.put("user", userObj);
StringEntity se = new StringEntity(holder.toString());
post.setEntity(se);
// setup the request headers
post.setHeader("Accept", "application/json");
post.setHeader("Content-Type", "application/json");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
response = client.execute(post, responseHandler);
json = new JSONObject(response);
} catch (HttpResponseException e) {
e.printStackTrace();
Log.e("ClientProtocol", "" + e);
json.put("info", "Email and/or password are invalid. Retry!");
} catch (IOException e) {
e.printStackTrace();
Log.e("IO", "" + e);
}
} catch (JSONException e) {
e.printStackTrace();
Log.e("JSON", "" + e);
}
return json;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment