Reputation: 338
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?
Upvotes: 2
Views: 1376
Reputation: 11497
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.
Upvotes: 3