2016-05-12 89 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

相關問題