0

I am having problems trying to POST a JSON Array.

For my Android code, I pass the JSON Array into the server by doing:

interests = // JSONArray of JSONObjects
final ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair(PARAM_USERNAME, username));
params.add(new BasicNameValuePair(PARAM_INTERESTS, interests.toString()));

HttpEntity entity =  new UrlEncodedFormEntity(params);
final HttpPost post = new HttpPost(UPDATE_INTERESTS_URI);
post.setEntity(entity);

// POST data to server

But when I read it from the server using:

$interests = $_POST["interests"];
echo $interets

It looks like [{\"a\":\"1\"},{\"b\":\"2\"}] instead of [{"a":"1"},{"b":"2"}]. The first one won't decode properly, and the second one works.

So why is it not working?

EDIT:
When I look at on Android before it posts, the JSONArray.toString() looks like [{"a":"1"},{"b":"2"}]

3 Answers 3

2

Don't know about android, but that looks like the magic quotes-feature of PHP is adding those slashes, if that's the case you could use this on server-side:

$interests = $_POST["interests"];
if (get_magic_quotes_gpc()) {
    $interests = stripslashes($interests);
}
echo $interests;
Sign up to request clarification or add additional context in comments.

Comments

1

do it in this way:

JSONObject paramInput = new JSONObject();
paramInput.put(PARAM_USERNAME, username);
paramInput.put(INTERESTS, interests.toString());
StringEntity entity = new StringEntity(paramInput.toString(), HTTP.UTF_8);

1 Comment

But would I have to change anything on the server side?
0

You can try to use:

StringEntity params = new StringEntity("your_Data");         

instead of your UrlEncodedEntity.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.