2

I try to send long array from android to PHP via JSON. I did the same thing with Javascript and worked but with JAVA it gets confusing. When I send the parameters, long array list changes.

This is the part that creates the list.

JSONArray list = new JSONArray();

for (int i = 0; i < users.size(); i++) {
    list.put(users.get(i).getId());                             
}

This is the code in JAVA that send the data.

public JSONObject sendFacebookFriendList(JSONArray list) {

    // Building Parameters
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("list", list.toString()));

    JSONParser jsonParser = new JSONParser();
    JSONObject json = jsonParser.getJSONFromUrl(accountServer, params);
    return json;
}

And this is the code that receives the data in PHP.

    $list = $_POST['list'];
    $result = array("success" => 1, "list" => $list);

While sending with Javascript the $list variable was becoming long array directly but I couldn't send it same way with JAVA.

When I send the list back to JAVA from PHP without any change I see that each array element has \" at the head and the end

So this list:

list= ["517565130","523709375","524503024","524620558","524965930", ...

becomes this:

"list":"[\"517565130\",\"523709375\",\"524503024\",\"524620558\", ...

So I cannot parse this array in PHP.

I couldn't find any way to send the long/int array in a proper way. I appreciate if someone can fix this or suggest another way.

Thanks

3
  • Try to configure your parser with ALLOW_BACKSLASH_ESCAPING_ANY CHARACTER Commented Feb 15, 2013 at 1:41
  • 1
    Why are you sending someone's friend-list to your own server? Commented Feb 15, 2013 at 2:23
  • I only let people login with facebook account now. So if they have any friends already using the app I set them friend in my DB too. That's why I do it. And solved the problem, thanks. Commented Feb 15, 2013 at 17:29

1 Answer 1

3

I solved the problem. The thing I skipped was decoding the json array that encoded at android part. So after getting the posted data it is needed to be decoded like this;

$list = $_POST['list'];

$obj = json_decode($list);

And then I can use $obj as array.

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

Comments

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.