0

I have problems trying to send an ArrayList from Android to PHP and then Insert this ArrayList into a MySQL table.

So, first of all I send List with all the parameters.

List<NameValuePair> list = new ArrayList<NameValuePair>();
        list.add(new BasicNameValuePair("tag", "registerCall"));
        list.add(new BasicNameValuePair("hourMatch", hourMatch));
        list.add(new BasicNameValuePair("dateMatch", dateMatch));
        list.add(new BasicNameValuePair("listPlayers", seleccionados
                .toString())); //ARRAYLIST<String>
        list.add(new BasicNameValuePair("idMatch", idMatch));

If I print the parameters list: [tag=registerCall, hourMatch=0931, dateMatch=2013-08-07, listPlayers=[8, 10], idMatch=7].

And then in the PHP, I collect the parameters and send it to the function.

if ($tag == 'registerCall') {

    $hourMatch = $_POST['hourMatch'];
    $dateMatch = $_POST['dateMatch'];
    $idMatch = $_POST['idMatch'];
    $listPlayers = $_POST['listPlayers'];


    $call = $db->registerCallMatch($dateMatch, $hourMatch, $idMatch, $listPlayers);

    if ($call != false) {

        $response["success"] = 1;
        $response["msg"] = "Insertados";

        echo json_encode($response);

    } else {

        $response["error"] = 1;
        $response["error_msg"] = "Error en la inserción";

        echo json_encode($response);
    }


}

And the function is:

public function registerCallMatch($dateCall, $timeCall, $idMatch, $listPlayers)
{
    $result = mysql_query("INSERT INTO call_match (date_call, time_call, idMatch) VALUES ('$dateCall', '$timeCall', '$idMatch')");

    if ($result != null) {

        $idCall = mysql_insert_id();

        if (is_array($listPlayers)) {

            foreach ($listPlayers as $idPlayer) {

                $result = mysql_query("INSERT INTO call_player (idCall, idPLayer) VALUES ('$idCall','$idPlayer')")or
                die ('unable' . mysql_error());

                return true;

            }

        } else {

            return false;

        }

    } else {

        return false;

    }

}

The problem is that the PHP doesn't recognize the ArrayList so I can't insert the strings into the database. But if I create an Array in the PHP, the function works well.

Edit: Ok. I found the problem. If I sent a string, I can´t use it as a Array. Now I have to convert the string into and array. Any idea? This is the string: [8, 10]. I have to put 8 and 10 in array.

Edit: I found the solution.

We have an ArrayList and we have to create a string. We can use a lot of methods but I use:

Iterator it = seleccionados.iterator();
        StringBuilder listIdPlayers = new StringBuilder();
        int i = 0;
        while (it.hasNext()) {
            if (i > 0) {
                listIdPlayers.append(" ").append(it.next());
                i++;
            } else {
                listIdPlayers.append(it.next());
                i++;
            }
        }

With this we have a string like this XX XX XX XX XX. With a space between the words.

Now we go to our php and we use the method explode(). With explode() we create the array.

$players = explode(" ", $listPlayers);

And then we check that the result is an array and iterate it.

if ($result != null) {

        $idCall = mysql_insert_id();

        if (is_array($players)) {

            foreach ($players as $idPlayer) {

                $result = mysql_query("INSERT INTO call_player (idCall, idPLayer) VALUES ('$idCall','$idPlayer')")or
                die ('unable' . mysql_error());

            }

            return true;


        }

Very important to put the return out of the foreach.

Thank you

3
  • First you can check whether data is getting or not at php side using echo. then you have proceeds further. Commented Aug 7, 2013 at 10:53
  • How can I make that PHP recognize the ArrayList that I sent in the parameters. I will check it Patrik. Thank you. Commented Aug 7, 2013 at 10:54
  • How you send java code to php, can you put your code here. Commented Aug 7, 2013 at 11:16

2 Answers 2

1

I think the problem is the way you send the data to the server. You send the Java object to php and it can't recognize it.

Convert it to a string with special dividing characters and explode it in php(bad practice).

OR

I solved such a problem in C# on following way (i am pretty sure that there is in java a similar way):

MultipartFormDataContent form = new MultipartFormDataContent();
form.Add(new StringContent(this.date_start), "data[DateSearch][date_start]");
form.Add(new StringContent(this.date_end), "data[DateSearch][date_end]");

You can see the declaration with "[...]" that is recognized as array content. So maybe you can find a way for your code:

MultipartFormDataContent list = new MultipartFormDataContent();
list.add("registerCall","data[tag]"));
list.add(hourMatch.ToString(), "data[hourMatch]"));
...
Sign up to request clarification or add additional context in comments.

4 Comments

But I'm sending the ArrayList like a string. Because I'm doing arraylist.toString();
Then check the data php is receiving. You have to do some operations to get an array out of this string. One (PHP) does not simply convert an array out of a string without an explicit function ;)
Ok. You help me a lot with it. Now I have to create the array since the string. Thank you. Any idea?
easiest way is to define a special unique seperator to divide the array elements in java and php (in fact the same). So you implode the array elemts in java with it and explode it in php
0

PHP does not recognise Java Objects, convert the List to an XML or JSON String and post it to the server. On the PHP side you can parse the string using a standard PHP XML/JSON parser.

1 Comment

Yeah but I send the ArrayList like a String because I made arraylist.toString()

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.