I'm having some issue with the post method: It seems the data is not being added to the request. My json object (jObject below) is {"FirstName":"John"} but the response is {"FirstName":"Peter"}. Why isn't POST working with this request?
@Override
protected Void doInBackground(Void... arg) {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost request = new HttpPost(someUrl);
ResponseHandler <String> responseHandler = new BasicResponseHandler();
request.setHeader("Content-type", "application/json");
StringEntity se = new StringEntity(jObject.toString());
se.setContentEncoding("UTF-8");
se.setContentType("application/json");
request.setEntity(se);
String authorizationString = "Basic " + Base64.encodeToString(("username" + ":" + "password").getBytes(), Base64.NO_WRAP);
request.addHeader("Authorization", authorizationString);
String response = httpClient.execute(request, responseHandler);
Edit: The way I originally retrieve the data
// Initialize url and create connection
URL url = new URL(urlSpec);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
byte[] auth = (email_login + ":" + password_login).getBytes();
String basic = Base64.encodeToString(auth, Base64.NO_WRAP);
httpConn.setRequestProperty("Authorization", "Basic " + basic);
// Retrieve data
try{
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream in = connection.getInputStream();
if(connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
return null;
}
int bytesRead = 0;
byte[] buffer = new byte[1024];
while ((bytesRead = in.read(buffer)) > 0) {
out.write(buffer, 0, bytesRead);
}
out.close();
return out.toByteArray();
} finally {
connection.disconnect();
}
{"FirstName":"John", "LastName":"Doe",...}. The response is{"FirstName":"Peter", "LastName":"Doe",...}