gpt4 book ai didi

java - 将 NSData 转换为 Java 字符串

转载 作者:行者123 更新时间:2023-11-29 02:42:21 24 4
gpt4 key购买 nike

我正在开发一个 iOS 应用程序,它使用用 java 编写的网络服务。该应用程序向该 Web 服务器发出 POST 请求,发送一个从 JSON 创建的 NSData 对象,例如:

<7b22636c 69656e74 223a7b22 656e7669 726f6e6d 656e7422 3a227361 6e64626f 78222c22 70726f64 7563745f 6e616d65 223a2250 61795061 6c20694f 53205344 4b222c22 70617970 616c5f73 646b5f76 65727369 6f6e223a 22322e32 2e31222c 22706c61 74666f72 6d223a22 694f5322 7d2c2272 6573706f 6e73655f 74797065 223a2270 61796d65 6e74222c 22726573 706f6e73 65223a7b 22696422 3a225041 592d3233 44393230 32375545 36383337 3034304b 51434337 5859222c 22737461 7465223a 22617070 726f7665 64222c22 63726561 74655f74 696d6522 3a223230 31342d30 392d3031 5430383a 33353a34 335a222c 22696e74 656e7422 3a227361 6c65227d 7d>

所以在我的 servlet 中我这样做

String req = request.getParameter("data");

但很明显,当我尝试打印它时,我得到了上面的字节。

如何将此类数据转换为初始 JSON?

编辑 1:我用这种方式发送一个NSData

    NSData *confirmation = [NSJSONSerialization dataWithJSONObject:completedPayment.confirmation
options:0
error:nil];

NSURL *url = [NSURL URLWithString:@"MY_WS"];

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *messageBody = [NSString stringWithFormat:@"data=%@", confirmation];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:[messageBody dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

我想我不能直接发送 JSON 字符串...

最佳答案

不确定 NSData 但如果你可以获得 byte[] 数组,你可以将它传递给 sting 构造函数。

String javaStringRep = new String(theByteArray, "UTF-8");

希望这会有所帮助..:)

关于java - 将 NSData 转换为 Java 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25602284/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com