2011-10-11 31 views
0

我正在使用适用于Android的Urban飞艇推送通知。在这一点上,我想使用广播发送通知所有我的用户。在使用它时,我得到了400个错误的请求错误。JSON载荷URL发布android

请告诉我,什么是错在我的代码:

Authenticator.setDefault(new Authenticator() { 
        protected PasswordAuthentication getPasswordAuthentication() { 
         return new PasswordAuthentication("sixxxxxxw","YSxxxxxxxxxx:Fxxxxxxxxxxxx".toCharArray()); 
         } 
       }); 

      URL url = new URL("https://go.urbanairship.com/api/airmail/send/broadcast/"); 
      HttpsURLConnection connection = (HttpsURLConnection)url.openConnection(); 
      connection.setRequestMethod("POST"); 
      connection.setDoOutput(true); 
      connection.setDoInput(true); 
      connection.setUseCaches(false); 


      connection.setRequestProperty("Content-Type","application/json"); 
      //connection.setRequestProperty("Content-Length", Integer.toString(data.length())); 
      JSONObject json = new JSONObject(); 
      json.put("title","My title"); 
      json.put("message","My message"); 



      try { 
       output = connection.getOutputStream(); 
       output.write(json.toString().getBytes()); 
      } finally { 
       if (output != null) { output.close(); } 
      } 

      int status = ((HttpURLConnection) connection).getResponseCode(); 
      System.out.println("status is..." + status); 

实际JSON Payload,我想送的是:

{ 
    "push": { 
     "aps": { 
      "alert": "New message!" 
     } 
    }, 
    "title": "Message title", 
    "message": "Your full message here.", 
    "extra": { 
     "some_key": "some_value" 
    } 
} 

或者此外,如果有sample code for using urban airpship push notifications broadcast api请分享这里。

如何发送此​​通过使用HttpsURLConnection服务。

谢谢

回答

1

这是你如何“打造”您的JSON有效载荷:

 JSONObject json = new JSONObject(); 
     JSONObject push = new JSONObject(); 
     JSONObject aps = new JSONObject(); 
     JSONObject extra = new JSONObject(); 
     aps.put("alert", "New message!"); 
     push.put("aps", aps); 
     json.put("push", push); 
     json.put("title","My title"); 
     json.put("message","My message"); 
     extra.put("some_key","some_value"); 
     json.put("extra", extra); 
+0

不过我得到响应代码400 ... – Udaykiran

+0

我加json.put (“额外”,额外);也在底部仍然是相同的错误 – Udaykiran

+0

你确定你的网址是否正确吗?它应该以类似.../boardcast.json –

0

您在代码中创建的JSON只包含标题和消息。缺乏

"push": { 
    "aps": { 
     "alert": "New message!" 
    } 
}, 

"extra": { 
    "some_key": "some_value" 
} 

不是吗?

+0

是,但我thght这是必要的将发送值.. – Udaykiran