Karly
Karly

Reputation: 389

Android : Array in a php POST

I'm doing an app that interacts with a mysql database with some php scripts. I would like to know if it is possible to send and array in a php POST from an android activity ?

This is th code I'm using for the moment :

        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

    nameValuePairs.add(new BasicNameValuePair("userID", String.valueOf(userID)));

but I think sending and array would be much better than sending one ID at a time.

Upvotes: 1

Views: 2739

Answers (2)

Karly
Karly

Reputation: 389

I saw a part of the solution in here :

enter link description here

In his example he simply does a for loop like this :

    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

            //friendID is an array of friends ID   
    for (int i=0;i<friendID.length;i++){
        nameValuePairs.add(new BasicNameValuePair("userAsked[]", String.valueOf(friendID[i])));

    }

The thing is : how do I use it in my php file ? Is it as simple as that :

$asked[] = $_POST['userAsked[]'];

Seems a little bit too easy :s

Upvotes: 0

Jan
Jan

Reputation: 400

I would generate a JSON/XML document. You can easily generate especially JSON objects in Android platform.

There are many examples about generating JSON in Android and reading/parsing JSON in PHP.

Hope this helps.

Upvotes: 3

Related Questions