Passage de tableau de Chaîne de PHP en tant que POSTE de

Je suis en train de passer un tableau de chaînes à un script PHP en tant que données POST, mais je suis pas sûr de quoi faire.

Voici mon code pour l'exécution de scripts PHP pour l'instant:

Où je suis en train de passer le tableau:

nameValuePairs.add(new BasicNameValuePair("message",message));
String [] devices = {device1,device2,device3};
nameValuePairs.add(new BasicNameValuePair("devices", devices));//<-- Can't pass String[] to BasicNameValuePair
callPHPScript("notify_devices", nameValuePairs);

Appel de script PHP:

public String callPHPScript(String scriptName, List<NameValuePair> parameters) {
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("http://localhost/" + scriptName);
    String line = "";
    StringBuilder stringBuilder = new StringBuilder();
    try {
        post.setEntity(new UrlEncodedFormEntity(parameters));

        HttpResponse response = client.execute(post);
        if (response.getStatusLine().getStatusCode() != 200)
        {
            System.out.println("DB: Error executing script !");
        }
        else {
            BufferedReader rd = new BufferedReader(new InputStreamReader(
                response.getEntity().getContent()));
            line = "";
            while ((line = rd.readLine()) != null) {
                stringBuilder.append(line);
            }
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("DB: Result: " + stringBuilder.toString());
    return stringBuilder.toString();
}

Et le script PHP en question:

<?php
include('tools.php');
//Replace with real BROWSER API key from Google APIs
$apiKey = "123456";

//Replace with real client registration IDs 
$registrationIDs = array($_POST[devices]); <-- Where I want to pass array to script

//Message to be sent
$message = $_POST['message'];

//Set POST variables
$url = 'https://android.googleapis.com/gcm/send';

$fields = array(
                'registration_ids'  => $registrationIDs,
                'data'              => array( "message" => $message ),
                );

$headers = array( 
                    'Authorization: key=' . $apiKey,
                    'Content-Type: application/json'
                );

//Open connection
$ch = curl_init();

//Set the url, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, $url );

curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );

//Execute post
$result = curl_exec($ch);

//Close connection
curl_close($ch);

print_as_json($result);
?>

Des idées? Merci !

Modifier

Je suis en train d'essayer ce qui suit, mais toujours pas de joie:

public void notifyDevices(Message message) {

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    List<String> deviceIDsList = new ArrayList<String>();
    String [] deviceIDArray;

    //Get devices to notify
    List<JSONDeviceProfile> deviceList = getDevicesToNotify();

    for(JSONDeviceProfile device : deviceList) {
        deviceIDsList.add(device.getDeviceId());
    }

    //Array of device IDs
    deviceIDArray = deviceIDsList.toArray(new String[deviceIDsList.size()]);
    for(String deviceID : deviceIDArray) {

        nameValuePairs.add(new BasicNameValuePair("devices[]", deviceID));

    }

    //Call script
    callPHPScript("GCM.php", nameValuePairs);
}

C'est tout le "rapport d'Erreurs" que j'ai...

        HttpResponse response = client.execute(post);
        if (response.getStatusLine().getStatusCode() != 200)
        {
            System.out.println("DB: Error executing script !");
        }
Que diriez -nameValuePairs.add(new BasicNameValuePair("devices[]", device1));, nameValuePairs.add(new BasicNameValuePair("devices[]", device2)); ... ?
vous devriez poster que comme une réponse.
Je vais vous donner que d'essayer maintenant, merci !

OriginalL'auteur TomSelleck | 2013-04-06