2011-05-11 7 views
0

Javaのソケットを使用してクライアントアプリケーションからxmlメッセージをサーバーに送信しようとしていますが、ストリームに書き込む方法がわかりません:ObjectOutputStream(socket.getOutputStream)へのStax XMLの書き込みMalformedByteSequenceException

 String user = "Oscar" 
     String pass = "1234" 

     ObjectOutputStream oos = new ObjectOutputStream(
       socket.getOutputStream()); 

     // Create a XMLOutputFactory 
     XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); 

     // Create XMLEventWriter 
     XMLEventWriter eventWriter = outputFactory 
         .createXMLEventWriter(oos); 

     // Create a EventFactory 
     XMLEventFactory eventFactory = XMLEventFactory.newInstance(); 

     XMLEvent end = eventFactory.createDTD("\n"); 

     // Create and 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()); 

私が使用する必要があります。

 eventWriter.flush(); 

私がしなければ、それは私に次の例外を与える:

javax.xml.stream.XMLStreamException:com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException:無効バイト1 1バイトのUTF-8シーケンス。

私は間違ってメッセージを書いていると言います。次に、書き込み方法は何ですか?

メッセージは次のようになります。

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

そして、ここでは、私がメッセージを読むために、サーバーで使用しているコードです:

//the call 
this.Interpreter.readCommand(socket.getInputStream()); 

//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 i call the right method for the data received 

} 

答えて

0

ObjectOutputStreamを使用しないでください。これはシリアル化のためのものです。あなたはシリアル化をしていません。 XMLEventWriterにソケットのストリームに直接書き込ませてください。同様に、読み取り側にObjectInputStreamを持たないでください。

これが失敗する理由は、ObjectOutputStreamがストリームにヘッダーを追加し、XMLパーサがそのストリームで窒息しているためです。

+0

ご協力いただきありがとうございます。 – OscarVGG

1

ドン」ストリームをObjectOutputStreamでラップします。あなたが必要としないストリームに余分な情報を追加しています(少なくとも、あなたの現在のコードに従って)。あなたがそれを必要としたとしても、このように使うことはできませんでした。

注記/原則として、入力ストリームと出力ストリームのセットアップは通常一致する必要があります。何らかの理由でとObjectOutputStreamを使用する必要がある場合は、それに合わせてもう一方の端にObjectInputStreamが必要です。

+0

あなたは右もトム・アンダーソンが最初にanwseredどこに。とにかく助けてくれてありがとう – OscarVGG

+0

実際、jtahlbornは私より数秒先だったと思います。オスカー、いくつかの人々は、同じように良い答えがいくつか得られたら、あなたは1つを受け入れるべきですが、他のものをupvoteするべきだと提案します。それはすべての答えに何らかの認識を与える方法です。 –

+0

ええ、私は最初だと思うけど、人生は続く。 :) – jtahlborn

関連する問題