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

4

2 回答 2

1

我认为问题在于您将数据发送到服务器的方式。您将 Java 对象发送到 php,但它无法识别它。

将其转换为具有特殊分隔符的字符串并在 php 中将其分解(不好的做法)。

或者

我通过以下方式在 C# 中解决了这样的问题(我很确定在 java 中有类似的方式):

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

您可以看到带有“[...]”的声明,该声明被识别为数组内容。所以也许你可以为你的代码找到一种方法:

MultipartFormDataContent list = new MultipartFormDataContent();
list.add("registerCall","data[tag]"));
list.add(hourMatch.ToString(), "data[hourMatch]"));
...
于 2013-08-07T11:33:58.523 回答
0

PHP 不识别 Java 对象,将 List 转换为 XML 或 JSON 字符串并将其发布到服务器。在 PHP 端,您可以使用标准的 PHP XML/JSON 解析器解析字符串。

于 2013-08-07T11:27:22.773 回答