【问题标题】:send push via urban airship using their web service (java)使用他们的网络服务(java)通过城市飞艇发送推送
【发布时间】:2012-01-18 10:21:13
【问题描述】:

我已经通过post

代码对我来说很好用。我需要使用 java 来执行此操作,我尝试使用 HttpURLConnection 和 javax.xml.rpc.Service 但没有运气。

我需要知道如何使用 java 来实现。

【问题讨论】:

    标签: push urbanairship.com


    【解决方案1】:

    解决了。

    pushClient 类:

    public static void main(String[] args)
    {
        try
        {
            String responseString = "";
            String outputString = "";
            String username = "Application Key";
            String password = "Application secret";
            Authenticator.setDefault(new MyAuthenticator(username,password));
    
            URL url = new URL("https://go.urbanairship.com/api/push/");
            URLConnection urlConnection = url.openConnection();
            HttpURLConnection httpConn = (HttpURLConnection)urlConnection;
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
    
            String postdata = "{\"android\": {\"alert\": \"Hello from JAVA!\"}, \"apids\": [\"APID\"]}";
            byte[] buffer = new byte[postdata.length()];
            buffer = postdata.getBytes("UTF8");
    
            bout.write(buffer);
            byte[] b = bout.toByteArray();
            httpConn.setRequestProperty("Content-Length",String.valueOf(b.length));
    
            httpConn.setRequestProperty("Content-Type", "application/json");
            httpConn.setRequestMethod("POST");
    
            httpConn.setDoOutput(true);
            httpConn.setDoInput(true);
    
            OutputStream out = httpConn.getOutputStream();
            out.write(b);
            out.close();
    
            InputStreamReader isr = new InputStreamReader(httpConn.getInputStream());
            BufferedReader in = new BufferedReader(isr);
    
            while ((responseString = in.readLine()) != null) 
            {
                outputString = outputString + responseString;
            }
            System.out.println(outputString);
    
        }
        catch (MalformedURLException e) 
        {
            e.printStackTrace();
        }
        catch (IOException e1) 
        {
            e1.printStackTrace();
        }
    }
    

    MyAuthenticator 类:

        private String user;
        private String passwd;
    
        public MyAuthenticator(String user, String passwd)
        {
            this.user = user;
            this.passwd = passwd;
        }
    
        protected PasswordAuthentication getPasswordAuthentication()
        {
            return new PasswordAuthentication(user, passwd.toCharArray());
        }
    

    【讨论】:

    • 检查互联网是否正常工作记得添加
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多