java - Writing Stax XML to ObjectOutputStream(socket.getOutputStream) getting MalformedByteSequenceException -


i'm trying send xml message server client app using sockets in java don't know how write stream:

        string user = "oscar"         string pass = "1234"          objectoutputstream oos = new objectoutputstream(                 socket.getoutputstream());          // create xmloutputfactory         xmloutputfactory outputfactory = xmloutputfactory.newinstance();          // create xmleventwriter         xmleventwriter eventwriter = outputfactory                         .createxmleventwriter(oos);          // create eventfactory         xmleventfactory eventfactory = xmleventfactory.newinstance();          xmlevent end = eventfactory.createdtd("\n");          // create , write start tag         eventwriter.add(eventfactory.createstartdocument());         eventwriter.add(end);          //se crean los atributos del tag         arraylist<attribute> attributes = new arraylist<attribute>();         attributes.add(eventfactory.createattribute("nickname", user));         attributes.add(eventfactory.createattribute("password", pass));          eventwriter.add(eventfactory.createstartelement             ("", "", "authenticate",attributes.iterator(), null));         eventwriter.add(end);          eventwriter.add(eventfactory.createendelement("", "", "authenticate"));          eventwriter.add(eventfactory.createenddocument()); 

should use:

        eventwriter.flush(); 

?

if do, gives me following exception:

javax.xml.stream.xmlstreamexception: com.sun.org.apache.xerces.internal.impl.io.malformedbytesequenceexception: invalid byte 1 of 1-byte utf-8 sequence.

it says i'm writing wrong message. then, write way?

the message should be:

<?xml version="1.0" encoding="utf-8"?>" + <authenticate nickname="oscar" password="1234" /> 

and here code i'm using @ server read message:

//the call this.interpreter.readcommand(socket.getinputstream()); 

and

//the method @suppresswarnings({ "unchecked", "null" }) public void readcommand(inputstream command) {      try {          // fabrica de entrada de xml         xmlinputfactory inputfactory = xmlinputfactory.newinstance();          // se crea un nuevo eventreader         xmleventreader eventreader = inputfactory.createxmleventreader(command);          while (eventreader.hasnext()) {              xmlevent event = eventreader.nextevent();              if (event.isstartelement()) {                  startelement startelement = event.asstartelement();                  // si tenemos un comando authenticate                 if (startelement.getname().getlocalpart().equals("authenticate")) {                      commandauthenticate(startelement);                 }              }          }      }     catch (xmlstreamexception e) {         e.printstacktrace();     } }   public void commandauthenticate (startelement startelement){       string nickname = null;     string password = null;      // se leen los atributos del comando     iterator<attribute> attributes = startelement                     .getattributes();      while (attributes.hasnext()) {          attribute attribute = attributes.next();          if (attribute.getname().tostring().equals("nickname")) {              nickname = attribute.getvalue();         }          if (attribute.getname().tostring().equals("password")) {              password = attribute.getvalue();         }      }      //here call right method data received  } 

don't use objectoutputstream. that's serialization. you're not doing serialization. have xmleventwriter write directly socket's stream. similarly, don't have objectinputstream on reading side.

the reason fails objectoutputstream adds header stream, xml parser choking on.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -