1

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();
    }
5
  • Are you sure that the request your sending is actually sending "Peter"? The code you have posted looks correct, could this be a server side issue? Commented Sep 5, 2013 at 0:54
  • Yes. There are other fields but this is the only one I'm editing. Commented Sep 5, 2013 at 1:37
  • Example. jObject: {"FirstName":"John", "LastName":"Doe",...}. The response is {"FirstName":"Peter", "LastName":"Doe",...} Commented Sep 5, 2013 at 1:43
  • You are saying the response from the server shows "Peter", are you sure that the server code is correct and not always returning "Peter" for some reason? I would also take a look at Charles Proxy to confirm the data being sent to the server is wrong. What doesn't make sense is if your json object has John like you say how would the request switch it with Peter? I don't think it would be possible that an http post problem would have somehow changed only some of the text in your request and not only kept the json structure intact but also inserted another random persons name. Commented Sep 5, 2013 at 1:55
  • See my edit, it may be helpful. This is how I retrieve the original Json which includes "Peter". (so I can get the data and do something with it). The idea is for someone to log-in and have the ability to change their info, like the first name. Once they do, I'm trying to recapture it into a Json object and send the data as a request, which was my original question. Commented Sep 5, 2013 at 2:09

1 Answer 1

1

Try setting the json data encoded in UTF8 byte array, like this:

request.setEntity(new ByteArrayEntity(
postMessage.toString().getBytes("UTF8")));

This works well for me...

Sign up to request clarification or add additional context in comments.

3 Comments

So get rid of all the StringEntity se
Can you check on server side what is getting there?
The response? It's a Json string but it doesn't update the data field. It still reads "Peter" instead of "John".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.