Hank
Hank

Reputation: 3497

Android POST JSON Array to Server

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"}]

Upvotes: 0

Views: 2829

Answers (3)

stewe
stewe

Reputation: 42654

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;

Upvotes: 2

waqaslam
waqaslam

Reputation: 68187

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);

Upvotes: 1

Kris
Kris

Reputation: 5792

You can try to use:

StringEntity params = new StringEntity("your_Data");         

instead of your UrlEncodedEntity.

Upvotes: 0

Related Questions