2016-05-12 44 views
0

使用以下代码,我可以向Firefox发送通知。它工作正常。如何使用Java将推送通知发送到Firefox浏览器

public static void main(String args[]){ 
      HttpClient httpClient = new DefaultHttpClient(); 
      try { 
       String furl = "https://updates.push.services.mozilla.com/push/v1/gAAAAABXM0VF8ptA2Tl38W4b9g9Dpm0Ek2G1ZTztq0USYSYKiOicOkcB0XEewj_Oh0UnwRbia8mMxmfoWL1kyw4ix5PKFS943hYbOo-PGqpPe7qkp4GYC4wk2Mk8Yy1Yyuj6FhM_S-ec"; 

       HttpPut request = new HttpPut(furl); 
       HttpResponse response = httpClient.execute(request); 

       Integer responseCode = response.getStatusLine().getStatusCode(); 
       System.out.println(responseCode); 
      }catch(Exception e){ 
       e.printStackTrace(); 
      } 
     } 

但是,当我试图发送标题,正文和网址uanble发送到Firefox。我得到的答复代码为400

 public static void main(String args[]){ 
      HttpClient httpClient = new DefaultHttpClient(); 
      try { 
    String furl = "https://updates.push.services.mozilla.com/push/v1/gAAAAABXM0VF8ptA2Tl38W4b9g9Dpm0Ek2G1ZTztq0USYSYKiOicOkcB0XEewj_Oh0UnwRbia8mMxmfoWL1kyw4ix5PKFS943hYbOo-PGqpPe7qkp4GYC4wk2Mk8Yy1Yyuj6FhM_S-ec"; 
       JSONObject msg = new JSONObject(); 
       msg.put("title", "Title"); 
       msg.put("body", "Body"); 
       JSONObject json = new JSONObject(); 
       json.put("data", msg); 
       HttpPut request = new HttpPut(furl); 

       request.setHeader("Content-type", MediaType.APPLICATION_JSON); 
       StringEntity params = new StringEntity(json.toString());   
       request.setEntity(params); 
HttpResponse response = httpClient.execute(request); 
       Integer responseCode = response.getStatusLine().getStatusCode(); 
       System.out.println(responseCode); 
      }catch(Exception e){ 
       e.printStackTrace(); 
      } 
     } 

任何人都可以帮助我解决这个问题吗?

+0

在你的第二个代码片段,你执行你的设置页眉和实体属性之前,您的请求。 – f1sh

+0

很抱歉,它错别字..我在设置标题后执行代码。我已更新代码 – Thulasiram

回答

0

我认为这是在你的代码一个错字:内容 - 牛逼 YPE

request.setHeader("Content-Type", MediaType.APPLICATION_JSON); 
+0

我尝试过..但不工作.. :-( – Thulasiram

+0

你是否得到相同的错误? – Vijay

+0

没有..当我使用第二个代码段时,状态代码为400 .. – Thulasiram

相关问题