eluleci
eluleci

Reputation: 3529

how to send long array from android to php server

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

Upvotes: 2

Views: 5447

Answers (1)

eluleci
eluleci

Reputation: 3529

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.

Upvotes: 3

Related Questions