我将iOS项目中的三维数组数据发送到套接字服务器java代码。 我以字节格式发送。 Socket在这一行接收数据,而((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());
}
如何在套接字代码中获取正确的数组数据?请指教!
答案 0 :(得分:1)
您应该使用某种形式的中间格式,如JSON或XML。 archivedDataWithRootObject:
生成二进制plist - 这是一种专有的Apple数据格式。
查看能够将NSArray,NSDictionary,NSNumber等存档到JSON中的NSJSONSerialization class - 这是一种人类和计算机可读格式。
我对Java并不了解,但我非常确定它有一个JSON解析器。
示例:
NSData *data = [NSJSONSerialization dataWithJSONObject:dataArray
options:0
error:nil];
data
现在包含您应发送到服务器的数据。