使用他们的web服务(java)通过城市飞艇发送推送



我已经完成了后

代码对我来说很好。我需要使用java来完成这项工作,我尝试过使用HttpURLConnection和javax.xml.rpc.Service,但没有成功。

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

解决了它。

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());
    }

相关内容

最新更新