2016-07-24 105 views
0

我使用Gcm多播消息傳遞HTTP,我提供任意數量的Ids,併發送消息... 我的第一個多播包含35個ID(35個目標用戶) ... 第二和第三,繼續播只包含7 IDS,但我仍然收到35條消息(而不是7)發送我發送7條消息拋出GCM並收到35條消息的回覆

代碼通過HTTP響應:

try{ 
HttpURLConnection connection = (HttpURLConnection) new   URL("https://android.googleapis.com/gcm/send").openConnection(); 

connection.setDoOutput(true); 
connection.setRequestProperty("Content-type", "application/json"); 
connection.setRequestProperty("Authorization", "key=AIzaSyASH05wdoY42OkmCvGwEbkihKzcwRCfDnQ"); 

// Write to the connection 
    OutputStream output = connection.getOutputStream(); 
output.write(content.getBytes(charset));// content is a Json message with 7 IDs in the field (registration_ids:"xxxxx","xxxx",....) 
output.close(); 

InputStream inputStream = connection.getErrorStream(); 
if (inputStream == null) 
    inputStream = connection.getInputStream(); 

// Read everything from our stream 
BufferedReader responseReader = new BufferedReader(new  InputStreamReader(inputStream, charset)); 

String inputLine; 


while ((inputLine = responseReader.readLine()) != null) { 
    response.append(inputLine); 
} 
responseReader.close(); 
}catch(IOException io) 
{ 
System.err.println("Error3 "+io); 
} 
return response.toString(); 
} 
    try{ 
HttpURLConnection connection = (HttpURLConnection) new URL(http_url_old).openConnection(); 

connection.setDoOutput(true); 
connection.setRequestProperty("Content-type", "application/json"); 
connection.setRequestProperty("Authorization",  "key=AIzaSyASH05wdoY42OkmCvGwEbkihKzcwRCfDnQ"); 

// Write to the connection 

OutputStream output = connection.getOutputStream(); 
output.write(content.getBytes(charset)); 
output.close(); 

// Check the error stream first, if this is null then there have been no issues with the request 
InputStream inputStream = connection.getErrorStream(); 
if (inputStream == null) 
    inputStream = connection.getInputStream(); 

// Read everything from our stream 
BufferedReader responseReader = new BufferedReader(new InputStreamReader(inputStream, charset)); 

String inputLine; 


while ((inputLine = responseReader.readLine()) != null) { 
    response.append(inputLine); 
} 
responseReader.close(); 
}catch(IOException io) 
{ 
System.err.println("Error3 "+io); 
} 
return response.toString(); 
} 

但我接收響應的服務器這樣的響應:

{ 「multicast_id」:6414471465709567733, 「成功」:28, 「失敗」:7, 「canonical_ids」:0 「結果」:[{ 「message _.... ect

28 + 7 = 35,我只發送7條消息!!! 多次發送後回覆(35)只包含7個ID的多播!

如何獲得正確的迴應?

回答

1

是似乎畝故障:

response.append(inputLine); 

我追加新的結果使「響應」字符串保持增長

的解決方案:我每次發新的組播時重設串..