Erreur de charge utile XML lors de l'envoi de notifications push au téléphone Windwos depuis Java

StackOverflow https://stackoverflow.com/questions/19835917

Question

Je reçois l'erreur suivante, lorsque j'utilise le code suivant pour envoyer des notifications push à Windows Phone.

Une erreur de charge utile de notification push s'est produite. Le XML contient du XML non valide ou mal formaté ou le type de notification spécifié dans l'en-tête ne correspond pas au type de charge utile utilisé.

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

J'ai utilisé le code C # de ce lien comme référence http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202967%28v=vs.105%29.aspx

Était-ce utile?

La solution

Vous convertissez votre chaîne en tableau d'octets, en utilisant le codage UTF-8. Ensuite, vous créez un écrivain de flux, avec un encodage UTF-16, convertissez la valeur de vos octets en chaîne et écrivez-les dans le flux. Honnêtement, je ne vois pas comment cela pourrait fonctionner.

Essayez ceci à la place:

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

Si vous ne pouvez pas passer une chaîne au write Méthode, alors écrivez directement dans le flux de sortie:

     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();
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top