HTTP code 400 returned while communication with GC

2019-04-14 23:17发布

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?

1条回答
Animai°情兽
2楼-- · 2019-04-14 23:53

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...!!!!!

查看更多
登录 后发表回答