2

I want to POST an android array (or list) to a php page and then insert all element of the array in a mysql table. I'm using the AsyncHttpClient library.

Now I'm using this code:

    RequestParams params = new RequestParams();
                    for ( String element: elements) {                 
                                params.add("elements[]", element);       
                    }
AsyncHttpClient client = new AsyncHttpClient();

client.post(context, "https://aaaaa.php", params, new AsyncHttpResponseHandler() {
    @Override
    public void onSuccess(int statusCode, Header[] headers, byte[] response) {
        try {
            //mycode
    }

    @Override
    public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
    }

});

But I don't know if it is the correct method. How I can get my android array in a php page?

3
  • Just to be clear... The problem is on the PHP side? And a tip, link the lib you're using. Commented Aug 1, 2015 at 16:10
  • This is the library that I'm using: loopj.com/android-async-http The biggest problem is on the PHP side, but I'm not sure that I'm doing the right thing in android. Commented Aug 1, 2015 at 17:29
  • I got it working here. I'll update my answer. Commented Aug 1, 2015 at 20:04

1 Answer 1

3

Ok, it seems you're having some trouble on both ends. So let's solve them both.

First, on your android side, declare the internet permission on your AndroidManifest.xml file.

AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"/>

There's nothing wrong with the rest of your code, I've used exactly the same thing, and it worked just fine.

Now, to the PHP end. You want to receive the data through POST, for that you will need to use the super global variable $_POST to fetch that data, which you can read more about here. And later, I'm using mysqli to save that data on a database, you can read more about it here.

Your_file.php

<?php
    $elements = $_POST['elements'];

    if (isset($elements)) {
        $count = count($elements);
        $i = 0;
        $mysqli = new mysqli("hostName","user","password","DbName");

        for ($i = 0; $i < $count; $i++) {
            $element = $elements[$i];
            // Here you can call a function to save this $element on your database. Like the example below.


            if ($mysqli->connect_error) {
                die('Error while connecting to database');
            }
            $sql = "INSERT INTO Table (`column_name`) 
                    VALUES ('$element');";

            $mysqli->query($sql);
        }

        $mysqli->close();
    }
?>

That should solve your problem.

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

5 Comments

This solution don't work for me.. I wrong something in android?
You are very welcome! Don't forget to mark the question as accepted, and upvote it if you can ;)
@Mauker, I tried your solution and it worked also for me. I have only one question regarding the RequestParams and the order of the elements. I have params.add("elements[]", val1) and params.add("elements[]",val2). When I print in $elements[0] from the PHP code, I get val2 when I would expect val1. Any idea for this?
@Konstantinos I believe (unconfirmed) that you can't guarantee the order when you do that. One workaround would be to pass your data as a JSON, and on the PHP side you could read that JSON.
thanks for the reply. JSON seems to be the way to go.

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.