【问题标题】:XML payload error when sending push notifications to windwos phone from java从 java 向 windows phone 发送推送通知时出现 XML 有效负载错误
【发布时间】:2013-11-07 12:26:12
【问题描述】:

当我使用以下代码向 windows phone 发送推送通知时,出现以下错误。

发生推送通知负载错误。XML 包含无效或格式不正确的 XML,或者标头中指定的通知类型与使用的负载类型不匹配。

try {
            String channelUri = "http://db3.notify.live.net/throttledthirdparty/01.00/AQFrOsAuKMIrQ6_3k_u4ZLo5AgAAAAADAQAAAAQUZm52OkJCMjg1QTg1QkZDMkUxREQFBkVVTk8wMQ";
            URL url = new URL(channelUri);
        URLConnection uc = url.openConnection();

        String name="hello";
        String body="from junit ";

         String toastMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                    "<wp:Notification xmlns:wp=\"WPNotification\">" +
                       "<wp:Toast>" +
                            "<wp:Text1>" + name + "</wp:Text1>" +
                            "<wp:Text2>" + body + "</wp:Text2>" +
                            "<wp:Param>/Page2.xaml?NavigatedFrom=Toast Notification</wp:Param>" +
                       "</wp:Toast> " +
                    "</wp:Notification>";

        byte[] mesg = toastMessage.getBytes("UTF-8");


        uc.setRequestProperty("ContentType", "text/xml");
        uc.setRequestProperty("X-WindowsPhone-Target", "toast");
        uc.setRequestProperty("X-NotificationClass", "2");

        uc.setDoOutput(true);
        uc.setDoInput(true);

        OutputStreamWriter writer = new OutputStreamWriter(uc.getOutputStream(),"utf-8");
        writer.write(mesg.toString(),0,mesg.toString().length());
        uc.connect();

        Map<String, List<String>> map = uc.getHeaderFields();
        for (Map.Entry<String, List<String>> entry : map.entrySet()) {
            System.out.println("Key : " + entry.getKey() + 
                     " ,Value : " + entry.getValue());
        }

        writer.flush();
        writer.close();

        String res = this.getURLOutput(uc);
        System.out.print(res);

    } catch(Exception e) {
       System.out.println(e.getMessage());
       Assert.fail();
   }

我已使用此链接中的 C# 代码作为参考 http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202967%28v=vs.105%29.aspx

【问题讨论】:

  • writer.write(mesg.toString(),0,mesg.toString().length()); 我不是 Java 专家,但这行让我觉得很奇怪。将字节转换为字符串?为什么不直接把toastMessage的内容写在你的OutputStreamWriter里呢?

标签: java windows-phone-8 push-notification mpns


【解决方案1】:

您正在使用 UTF-8 编码将字符串转换为字节数组。然后创建一个流写入器,使用 UTF-16 编码,将字节的值转换为字符串,并将它们写入流中。老实说,我不明白它是如何工作的。

试试这个:

    String toastMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                "<wp:Notification xmlns:wp=\"WPNotification\">" +
                   "<wp:Toast>" +
                        "<wp:Text1>" + name + "</wp:Text1>" +
                        "<wp:Text2>" + body + "</wp:Text2>" +
                        "<wp:Param>/Page2.xaml?NavigatedFrom=Toast Notification</wp:Param>" +
                   "</wp:Toast> " +
                "</wp:Notification>";

    // Do your stuff, initialize the headers, and so on... 

    OutputStreamWriter writer = new OutputStreamWriter(uc.getOutputStream(), "utf-8");
    writer.write(toastMessage, 0, toastMessage.length());
    uc.connect();

如果不能将String传递给write方法,那么直接写在输出流中:

     String toastMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                "<wp:Notification xmlns:wp=\"WPNotification\">" +
                   "<wp:Toast>" +
                        "<wp:Text1>" + name + "</wp:Text1>" +
                        "<wp:Text2>" + body + "</wp:Text2>" +
                        "<wp:Param>/Page2.xaml?NavigatedFrom=Toast Notification</wp:Param>" +
                   "</wp:Toast> " +
                "</wp:Notification>";

    byte[] mesg = toastMessage.getBytes("UTF-8");

    uc.setDoOutput(true);
    uc.setDoInput(true);

    uc.getOutputStream().write(mesg, 0, mesg.length());
    uc.connect();

【讨论】:

  • @PrasannaAarthi 你什么意思,你还有同样的错误吗?
  • 是的,我遇到了同样的错误。我需要传递一个字节数组。这是 C# 等效的 requestStream.Write(notificationMessage, 0, notificationMessage.Length); (字节[],int,int)
  • @PrasannaAarthi 这很奇怪,这里列出的 write 方法的重载需要一个字符串:developer.android.com/reference/java/io/OutputStreamWriter.html
  • @PrasannaAarthi 无论如何,如果你想或需要写字节,不要使用OutputStreamWriter,直接使用底层流(见我的编辑)
猜你喜欢
  • 1970-01-01
  • 2019-08-30
  • 1970-01-01
  • 1970-01-01
  • 2016-07-19
  • 1970-01-01
  • 2012-12-23
  • 1970-01-01
  • 2021-04-26
相关资源
最近更新 更多