0

Java のソケットを使用してクライアント アプリからサーバーに xml メッセージを送信しようとしていますが、それをストリームに書き込む方法がわかりません。

        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 バイト UTF-8 シーケンスのバイト 1 が無効です。

それは私がメッセージを間違って書いていると言います。では、書き方は?

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

<?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

}
4

2 に答える 2

1

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

補足/一般的なルールとして、入力ストリームと出力ストリームの設定は通常一致する必要があります。何らかの理由でandObjectOutputStreamを使用する必要がある場合は、それに一致するようにもう一方の端にObjectInputStreamが必要になります

于 2011-05-11T14:33:43.687 に答える
0

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

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

于 2011-05-11T14:38:26.590 に答える