从iOS到Socket的数据损坏



我正在从我的iOS项目发送一个三维数组数据到套接字服务器java代码。我以字节格式发送。套接字正在接收这一行的数据,而((read = input.read(bufferr))> 0) {String s = new String(bufferr);}

但是输出看起来不是合适的可读文本。它就像下面。

Ä£ÄÄÄXUIButtonVSubmitS123“!X$classesZ$classname£ ^NSMutableArrayWNSArrayXNSObject^NSMutableArray“
$Ä£%&'ÄÄ    Ä
WUILabelYUser nameS340܆_NSKeyedArchiver(25:<HNSZegjlnsuy{}àèìò°¨∞ø«–fl‰ÊÍÏÓ¯-ˇ˛END
Found last byte END, which has appended by Client
decodedData []: [B@394a8cd1
While loop came out

iOS代码:

NSMutableArray *dataArray;
dataArray = [[NSMutableArray alloc] initWithCapacity:2];
[dataArray insertObject:[NSMutableArray arrayWithObjects :@"UIButton",@"Submit",@"123",nil] atIndex:0];
[dataArray insertObject:[NSMutableArray arrayWithObjects :@"UILabel",@"User name",@"340",nil] atIndex:1];

NSLog(@"dataArray:  %@", dataArray);
NSData *arrayData = [NSKeyedArchiver archivedDataWithRootObject:dataArray];
NSMutableData *data = [NSMutableData data];
data = [arrayData mutableCopy];
NSData *newData = [@"END" dataUsingEncoding:NSUTF16StringEncoding];
[data appendData:newData];

int num = [outputStream write:[data bytes] maxLength:([data length])];
if ( -1 == num )
{
    NSLog(@"Error writing to stream %@: %@", outputStream, [outputStream streamError]);
}
else
{
    NSLog(@"Wrote %i bytes to stream %@.", num, outputStream);
}

套接字服务器:

public SocketConnection(Socket socket, HashMap<String, byte[]> dataHashMap, ArrayList<HashMap<String, ClientInfo>> myList) {
        super("Thread 1");
        this.socket = socket;
        this.hm = dataHashMap;
        this.clientList = myList;
        try {
            pw = new PrintWriter(socket.getOutputStream(), true);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        System.out.println("Socket Called first time.");
        pw.println("SUCCESS");
        pw.flush();
        try {
            input = socket.getInputStream();
            scannerObj = new Scanner(socket.getInputStream()); 
            clientOutput = socket.getOutputStream();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
    }
    }
    @Override
    public void run() {
        do
        {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();               
            byte[] bufferr = new byte[1024];
            int read = 0;
            long numWritten = 0;
            try {
                while ((read = input.read(bufferr)) > 0) {
                    baos.write(bufferr, 0, read);
                    numWritten += read;
                    System.out.println("numWritten: " + numWritten);
                    String s = new String(bufferr);
                    System.out.println("s:" + s);
                    // If END found, just end the reading
                    if ( s.contains("END") )
                    {
                        System.out.println("decodedData []: " + bufferr.toString());
                        break;
                    }
                }
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }       
            try {
                baos.flush();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }       

        } while (scannerObj.hasNext());
    }

如何在我的套接字代码中获得适当的数组数据?请建议!

您可能应该使用某种形式的中间格式,如JSON或XML。archivedDataWithRootObject:生成一个二进制plist -这是一个专有的苹果数据格式。

看看NSJSONSerialization类,它能够将你的NSArray, NSDictionary, NSNumber等归档为JSON -这是一种人类和计算机可读的格式。

我不太了解Java,但我很确定它有一个JSON解析器。

的例子:

NSData *data = [NSJSONSerialization dataWithJSONObject:dataArray
                                               options:0
                                                 error:nil];

data现在包含了您应该发送到服务器的数据。

相关内容

  • 没有找到相关文章

最新更新