我试图将字符串数组作为POST数据传递给PHP脚本,但是不确定该怎么做。
这是到目前为止我执行PHP脚本的代码:
我在哪里尝试传递数组:
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);
调用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();
}
以及相关的PHP脚本:
<?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);
?>
有任何想法吗?谢谢 !
编辑
我正在尝试以下方法,但仍然不满意:
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);
}
这就是我所有的“错误报告” ...
HttpResponse response = client.execute(post);
if (response.getStatusLine().getStatusCode() != 200)
{
System.out.println("DB: Error executing script !");
}
参考方案
要将数组传递给查询字符串中的php,您应该在标识符中添加[]
并将每个项目添加为单独的条目,因此这样的工作应该可以:
nameValuePairs.add(new BasicNameValuePair("devices[]", device1));
nameValuePairs.add(new BasicNameValuePair("devices[]", device2));
nameValuePairs.add(new BasicNameValuePair("devices[]", device3));
现在,php端的$_POST['devices']
将包含一个数组。
在Java中,我们是否有任何方法可以发现特定字符串是字符串数组的一部分。我可以避免出现一个循环。例如String [] array = {"AA","BB","CC" }; string x = "BB" 我想要一个if (some condition to tell wheth…
Java Scanner读取文件的奇怪行为 - java因此,在使用Scanner类从文件读取内容时,我遇到了一个有趣的问题。基本上,我试图从目录中读取解析应用程序生成的多个输出文件,以计算一些准确性指标。基本上,我的代码只是遍历目录中的每个文件,并使用扫描仪将其打开以处理内容。无论出于何种原因,扫描程序都不会读取其中的一些文件(所有UTF-8编码)。即使文件不是空的,scanner.hasNextLine()在…
Java-如何将此字符串转换为日期? - java我从服务器收到此消息,我不明白T和Z的含义,2012-08-24T09:59:59Z将此字符串转换为Date对象的正确SimpleDateFormat模式是什么? java大神给出的解决方案 这是ISO 8601标准。您可以使用SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM…
Java Globbing模式以匹配目录和文件 - java我正在使用递归函数遍历根目录下的文件。我只想提取*.txt文件,但不想排除目录。现在,我的代码如下所示:val stream = Files.newDirectoryStream(head, "*.txt") 但是这样做将不会匹配任何目录,并且返回的iterator()是False。我使用的是Mac,所以我不想包含的噪音文件是.DS_ST…
直接读取Zip文件中的文件-Java - java我的情况是我有一个包含一些文件(txt,png,...)的zip文件,我想直接按它们的名称读取它,我已经测试了以下代码,但没有结果(NullPointerExcepion):InputStream in = Main.class.getResourceAsStream("/resouces/zipfile/test.txt"); Buff…