Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 发送gcm消息的错误请求_Android_Google Cloud Messaging - Fatal编程技术网

Android 发送gcm消息的错误请求

Android 发送gcm消息的错误请求,android,google-cloud-messaging,Android,Google Cloud Messaging,我正试图通过C.发送gcm消息 我尝试了好几次,但在尝试用json方法发送请求时,收到了http:400错误请求 当我试图以文本形式发送它时,我无法阅读它(rtl语言)-这就是为什么我要尝试JSON 有人知道问题出在哪里吗? 谢谢 私有静态字符串SendNotificationJson2(字符串id,字符串msg) { var AuthString=“AIzaSyDAtmaqSdutBQemqmd4dQgf33B_6ssbvXA”; var RegistrationID=id; var Mes

我正试图通过
C.
发送gcm消息

我尝试了好几次,但在尝试用json方法发送请求时,收到了
http:400错误请求

当我试图以文本形式发送它时,我无法阅读它(rtl语言)-这就是为什么我要尝试
JSON

有人知道问题出在哪里吗? 谢谢

私有静态字符串SendNotificationJson2(字符串id,字符串msg) { var AuthString=“AIzaSyDAtmaqSdutBQemqmd4dQgf33B_6ssbvXA”; var RegistrationID=id; var Message=msg; //--创建取代C2DM Web请求对象的GCM请求--// HttpWebRequest请求=(HttpWebRequest)WebRequest.Create(“https://android.googleapis.com/gcm/send"); Request.Method=“POST”; Request.KeepAlive=false; //--创建查询字符串--// Dictionary dict=新字典(); 添加(“注册号”,注册号); 添加(“数据”,信息); 添加(“折叠键”,“1”); 字符串postData=GetPostStringFrom(dict); byte[]byteArray=Encoding.UTF8.GetBytes(postData); Request.ContentType=“application/json”; Request.ContentLength=byteArray.Length; Request.Headers.Add(“Authorization”,“key=“+AuthString”); //--委托建模以验证服务器证书--// ServicePointManager.ServerCertificateValidationCallback+=委托( 对象 发件人, System.Security.Cryptography.X509Certificates.X509Certificate 证书, System.Security.Cryptography.X509Certificates.X509Chain pChain, System.Net.Security.SslPolicyErrors pSSLPolicyErrors) { 返回true; }; //--创建流以写入字节数组--// Stream dataStream=Request.GetRequestStream(); 写入(byteArray,0,byteArray.Length); dataStream.Close(); //--留言--// WebResponse=Request.GetResponse(); HttpStatusCode响应代码=((HttpWebResponse)响应); if(ResponseCode.Equals(HttpStatusCode.Unauthorized)| | ResponseCode.Equals(HttpStatusCode.unforbidded)) { 返回“未授权-需要新令牌”; } 如果(!ResponseCode.Equals(HttpStatusCode.OK))为else,则为 { 返回“来自web服务的响应不正常”; //WriteLine(“来自web服务的响应不正常:”); //Console.WriteLine(((HttpWebResponse)Response.StatusDescription); } StreamReader=新的StreamReader(Response.GetResponseStream()); 字符串responseLine=Reader.ReadLine(); Reader.Close(); 返回“ok”; } 私有静态字符串GetPostStringFrom(字典postFieldNameValue) { //返回Newtonsoft.Json.JsonConvert.SerializeObject(postFieldNameValue); 返回“\”数据\“:{\”消息\“:\”+postFieldNameValue[“数据”]+“\”},\“注册\u ID\”:[\”+postFieldNameValue[“注册\u ID”]+“\”}”; }

您的注册ID必须是JSON数组,因此请使用列表而不是单个字符串。

您忘记了JSON数据中的第一个括号

使用

而不是

return "\"data\": {\"Message\": \"" +  postFieldNameValue["data"] + "\"},\"registration_ids\":[\"" +  postFieldNameValue["registration_ids"] + "\"]}";
return "{\"data\": {\"Message\": \"" +  postFieldNameValue["data"] + "\"},\"registration_ids\":[\"" +  postFieldNameValue["registration_ids"] + "\"]}";
return "\"data\": {\"Message\": \"" +  postFieldNameValue["data"] + "\"},\"registration_ids\":[\"" +  postFieldNameValue["registration_ids"] + "\"]}";