与GCM通信时返回HTTP代码400 [英] HTTP code 400 returned while communication with GCM

查看:121
本文介绍了与GCM通信时返回HTTP代码400的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图为GCM通信实现HTTP服务器。提供正确的密钥后,我将HTTP响应代码设置为'400'。以下是代码段:

 网址url =新网址(https://android.googleapis.com/gcm/send ); 
Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress(proxy.sgp.com,8080));
HttpURLConnection conn =(HttpURLConnection)url.openConnection(proxy);
conn.setRequestMethod(POST);
conn.setRequestProperty(Content-Type,application / json);
conn.setRequestProperty(Authorization,key =+AIzaSyAYc53L6kg_XerwoWdLjUAi2iEfNKidSF8);
conn.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.writeUTF({\collapse_key \:\Food-Promo \,\data \:{\Category \:\FOOD \\ \\ \ Type\:\ VEG\,},\ registration_ids\:[\ APA91bEk7GPFVxzOidvB3JKCMWq3FHpAaTj2dBv9VGOQkKtLAEiVGR8TDi1fsU4D1k293ODAFTJ8dNfE2gzJNfCvB1sjewZu2fGOIJmY8dgjcNTZQYi4QfyQH-AaO0qEmQnbEeEtsUQ5LzWrIHboAhJMx1bfdsO9bg\]});
wr.flush();
wr.close();
int responseCode = conn.getResponseCode();

任何人都可以帮忙吗?

解决方案

仅在以下情况下才会出现400错误: 用于JSON请求。 表明请求不能被解析为JSON ,或者它包含无效的字段(例如,传递一个字符串到期望的数字)。答案中描述了确切的失败原因,并且在重新提出请求之前应解决问题。
参考资料



所以你可以试试这段代码,它为我工作



为GCM的标准消息格式创建一个POJO类

  public class Content implements Serializable {

public List< String> registration_ids;
public Map< String,String>数据;

public void addRegId(String regId){
if(registration_ids == null)
registration_ids = new LinkedList< String>();
registration_ids.add(regId);

$ b $ public void createData(String title,String message){
if(data == null)
data = new HashMap< String,String>() ;

data.put(title,title);
data.put(message,message);


$ b

要求GCM发送通知的代码消息给Android设备,

  String apiKey =; //由Google Console提供的API密钥
String deviceID =; // Device Id
Content content = new Content();
//标准消息格式的POJO类如上所示
content.addRegId(deviceID);
content.createData(标题,通知消息);
网址url =新网址(https://android.googleapis.com/gcm/send);
HttpURLConnection conn =(HttpURLConnection)url.openConnection();
conn.setRequestMethod(POST);
conn.setRequestProperty(Content-Type,application / json);
conn.setRequestProperty(Authorization,key =+ apiKey);
conn.setDoOutput(true);
ObjectMapper mapper = new ObjectMapper();
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
mapper.writeValue(wr,content);
wr.flush();
wr.close();
responseCode = conn.getResponseCode();

希望这可以帮助您解决您的问题...... !!!!!


I am trying to implement HTTP server for GCM communication. After providing the correct keys , I get HTTP response code as '400'. Here is the code snippet:

URL url = new URL("https://android.googleapis.com/gcm/send");
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.sgp.com", 8080));
        HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("Authorization", "key="+"AIzaSyAYc53L6kg_XerwoWdLjUAi2iEfNKidSF8");
        conn.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
        wr.writeUTF("{\"collapse_key\" : \"Food-Promo\", \"data\" : {\"Category\" : \"FOOD\",\"Type\": \"VEG\",}, \"registration_ids\": [\"APA91bEk7GPFVxzOidvB3JKCMWq3FHpAaTj2dBv9VGOQkKtLAEiVGR8TDi1fsU4D1k293ODAFTJ8dNfE2gzJNfCvB1sjewZu2fGOIJmY8dgjcNTZQYi4QfyQH-AaO0qEmQnbEeEtsUQ5LzWrIHboAhJMx1bfdsO9bg\"]}");
        wr.flush();
        wr.close();
        int responseCode = conn.getResponseCode();

Can anybody help?

解决方案

400 error only comes in following case:

Only applies for JSON requests. Indicates that the request could not be parsed as JSON, or it contained invalid fields (for instance, passing a string where a number was expected). The exact failure reason is described in the response and the problem should be addressed before the request can be retried. Reference

So you can try this code, its working for me

Create one POJO class for standard message format for GCM

public class Content implements Serializable {

public List<String> registration_ids;
public Map<String,String> data;

public void addRegId(String regId){
    if(registration_ids == null)
        registration_ids = new LinkedList<String>();
    registration_ids.add(regId);
}

public void createData(String title, String message){
    if(data == null)
        data = new HashMap<String,String>();

    data.put("title", title);
    data.put("message", message);
}

}

Code to request GCM to send notification message to android device,

   String apiKey = ""; //API key provided by Google Console
   String deviceID="";//Device Id
   Content content = new Content();
   //POJO class as above for standard message format
   content.addRegId(deviceID);       
   content.createData("Title", "Notification Message");
   URL url = new URL("https://android.googleapis.com/gcm/send");
   HttpURLConnection conn = (HttpURLConnection) url.openConnection();
   conn.setRequestMethod("POST");
   conn.setRequestProperty("Content-Type", "application/json");
   conn.setRequestProperty("Authorization", "key="+apiKey);
   conn.setDoOutput(true);
   ObjectMapper mapper = new ObjectMapper();
   DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
   mapper.writeValue(wr, content);
   wr.flush();
   wr.close();
   responseCode = conn.getResponseCode();

Hope this helps you with your problem...!!!!!

这篇关于与GCM通信时返回HTTP代码400的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆