[英]How can I send more than one String through a POST or JSON? Can I send a JSON array through another JSON object?
我正在嘗試為Android實現推送通知。 我想將同一封郵件發送到多個電話。 目前可以發送到一部手機。 我想從一個mysql數據庫中獲取注冊密鑰,並向所有這些電話發送一條消息。
問題是我對json和數組以及POST感到困惑。 我可以通過另一個JSON對象發送JSON數組嗎? 我怎樣才能放入params.add(new BasicNameValuePair("regId", regkeys));
不僅僅是一個注冊碼? 我試圖將regkeys變成一個數組,但是沒有用。 我知道它一定是某種形式
{ "message":"some message here", "regIds": [ "dsdfsfet366767547", "63567356366reygh", "sgdgtwetwetsdgsdg", "sdgsdgsdgwet24t" ] }
但我不知道如何創建它。
郵遞區號
$regId = $_POST["regId"];
$message = $_POST["message"];
...
$registatoin_ids = array($regId);
$message = array("price" => $message);
$result = $gcm->send_notification($registatoin_ids, $message);
...
public function send_notification($registatoin_ids, $message) {...}
Android代碼
String regkeys = regkey.getText().toString();
String message = messg.getText().toString();
...
List<NameValuePair> params = new ArrayList<NameValuePair> ();
params.add(new BasicNameValuePair("regId", regkeys));
params.add(new BasicNameValuePair("message", message));
JSONObject json = parser2.makeHttpRequest(url, params);
...
public class jParser2 {
static InputStream is = null;
static JSONObject jObject = null;
static String json = "";
public jParser2(){
}
public JSONObject makeHttpRequest(String url, List<NameValuePair> params){
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader (new InputStreamReader (is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine())!=null){
sb.append(line+"\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
jObject = new JSONObject(json);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jObject;
}
}
我的服務器要GCM代碼。
public function send_notification($registatoin_ids, $message) {
// include config
include_once './config.php';
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);
$headers = array(
'Authorization: key=' . GOOGLE_API_KEY,
'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);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
echo $result;
}
我不知道您的$gcm->send_notification
函數是什么樣的,但它應該具有以下內容:
$data = array( 'message' => 'some message' );
$ids = array( 'reg-id1', 'reg-id2' );
$payload = array(
'registration_ids' => $ids,
'data' => $data,
);
$headers = array(
'Authorization: key=' . $apiKey,
'Content-Type: application/json'
);
請注意,為了將同一條消息發送到多個注冊ID,請求的內容類型必須為JSON。 在包含數組的registration_ids
參數中發送注冊ID,並且通知數據在包含字典的data
參數內部。
是的,您可以將帶有regIds的數組放入JSON對象,並將JSON Objet放入Post中:
List<NameValuePair> params = new ArrayList<NameValuePair> ();
params.add(new BasicNameValuePair("json", JsonObject));
httpPost.setEntity(new UrlEncodedFormEntity(params));
希望這對您有所幫助。
我建議使用GSON 。
現在,使用創建一個類:
Class Messages {
public String message;
public ArrayList<String> regIDs;
public void setMessage(String message) {
this.message = message;
}
public ArrayList<String> getRegIDs() {
return regIDs;
}
}
現在,當您要將一些數據發送到服務器時,請創建此類的新對象。
Messages messages = new Messages();
messages.setMessage("Some message here");
//Add all the regn IDs to the regIDs List.
ArrayList<String> regIDs = messages.getRegIDs()
regIDs.add("regnID1");
regIDs.add("regnID2");
regIDs.add("regnID3");
regIDs.add("regnID4");
//Add as many IDs as you want.
//then create a new GSON Object.
Gson gson = new Gson();
String jsonString = gson.toJSON(messages);
現在, jsonString
將自動包含您指定格式的JSON字符串。 因此,將帶有jsonString
變量內容的POST請求發送到您的服務器。 然后在Php端,使用json_decode解碼為數組,並遍歷regIDs數組以發送多個gcm消息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.