【发布时间】:2014-10-21 09:00:24
【问题描述】:
我有一个 Java 服务器,想向 iOS 应用程序发送字符串消息。
发送理论上可行,但我总是在我的应用程序中收到“¬í”。我尝试了不同的编码,如 ASCII、Unicode、UTF-16。
我的 Java 发送方法如下所示:
public void sendName(String str) {
try {
System.out.println("Send: "+str);
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
oos.writeObject(str.getBytes(StandardCharsets.US_ASCII));
} catch (IOException ex) {
}
}
而我的目标 C 接收方法如下所示:
- (void)readFromStream{
uint8_t buffer[1024];
int len;
NSMutableString *total = [[NSMutableString alloc] init];
while ([inputStream hasBytesAvailable]) {
len = [inputStream read:buffer maxLength:sizeof(buffer)];
if (len > 0) {
[total appendString: [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding]];
NSLog(@"%@",total);
}
}
}
有人知道,怎么了? 谢谢你:)
【问题讨论】:
-
您是否真的尝试将 java 对象
String发送到您的 iOS 应用程序?听起来很大胆
标签: java ios networking stream