Reputation: 1868
I am sending a 3 dimensional array data from my iOS project to a socket server java code. I send in bytes format. Socket is receiving the data at this line, while ((read = input.read(bufferr)) > 0) { String s = new String(bufferr); }
But the output doesn't look proper readable text. It is coming like below.
Ä£ÄÄÄ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 code:
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);
}
Socket Server:
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());
}
How to get the proper array data in my socket code? Please advise!
Upvotes: 0
Views: 140
Reputation: 3793
You should probably use some form of intermediate format like JSON or XML. archivedDataWithRootObject:
generates a binary plist - which is a proprietary Apple data format.
Look at the NSJSONSerialization class which is able to archive your NSArray, NSDictionary, NSNumber, etc. into JSON - which is a human and computer readable format.
I don't know much about Java, but I'm pretty sure it has a JSON parser.
Example:
NSData *data = [NSJSONSerialization dataWithJSONObject:dataArray
options:0
error:nil];
The data
now contains the data you should sent to your server.
Upvotes: 1