使用以下代码,我可以向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();
}
}
任何人都可以帮助我解决这个问题吗?
在你的第二个代码片段,你执行你的设置页眉和实体属性之前,您的请求。 – f1sh
很抱歉,它错别字..我在设置标题后执行代码。我已更新代码 – Thulasiram