有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何使用Java向firefox浏览器发送推送通知

使用以下代码,我可以向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();
            }
        }

但当我试图将标题、正文和URL 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();
            }
        }

有人能帮我吗


共 (1) 个答案

  1. # 1 楼答案

    我认为你的代码中有一个输入错误:Content-Type

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