简体   繁体   English

NSMutableData到NSString-如何知道用于HTTP响应的编码

[英]NSMutableData to NSString - how to know which encoding to use for HTTP response

I'm using Google Protobuf to send a serialized class to an http server. 我正在使用Google Protobuf将序列化的类发送到http服务器。 The command to do this is: 为此的命令是:
message.SerializeToString(&out); Notice that we are serializing to a String. 请注意,我们正在序列化为String。 The server returns the exact same object back to me. 服务器将完全相同的对象返回给我。

So, in my connection: didReceiveData method, I am getting data. 因此,在我的连接:didReceiveData方法中,我正在获取数据。

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data
{
    if (self.receivingData) {
        [self.dataReceived appendData:data];
    }
}

In my connectionDidFinishLoading method I think I need to put the NSMutableData (self.dataReceived) into an NSString. 我认为在我的connectionDidFinishLoading方法中,需要将NSMutableData(self.dataReceived)放入NSString中。

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    self.receivingData = NO;
    NSLog(@"%@", self.dataReceived);

    NSString *data = [[NSString alloc] initWithData:self.dataReceived encoding:NSASCIIStringEncoding];  // Wrong encoding ????

    NSMutableDictionary *processedData = [NSMutableDictionary dictionaryWithCapacity:1];
    [processedData setObject:data forKey:@"ImageData"];

    NSNotificationCenter *processedNote = [NSNotificationCenter defaultCenter];
    [processedNote postNotificationName:@"DataReceived" object:nil userInfo:processedData];
}

But I'm not sure what encoding to use. 但是我不确定要使用哪种编码。 When I send the data, it looks like this: 当我发送数据时,它看起来像这样:

"\b\x01\x12\x04Lucy\x1a\xd4\xdc;\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\...... (There's more)

When I receive the data, it looks like this: 当我收到数据时,它看起来像这样:

<08011204 4c756379 1ad4dc3b ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ..... (There's more)

When I init the NSString with data above (encoding NSASCIIStringEncoding), I get this: 当我使用上面的数据初始化NSString时(对NSASCIIStringEncoding进行编码),我得到了:

LucyÔÜ;ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ ....... (There's more)

Ultimately, I will need to parse the data from a string using the Google Protobuf method: message.ParseFromString(data); 最终,我将需要使用Google Protobuf方法从字符串中解析数据: message.ParseFromString(data);

How can I know which encoding to use? 我怎么知道要使用哪种编码?

Try this. 尝试这个。

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@"%@", [response textEncodingName]);
}

I ended up using the following code: 我最终使用了以下代码:

    const char *bytes = (const char *)[data bytes];
    std::string byteString = std::string(bytes);

That worked!! 可行!

But @trick14 had a cool answer that shows the encoding type. 但是@ trick14有一个很酷的答案,显示了编码类型。 That was awesome. 这太棒了。

I hope this helps somebody. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM