1

我正在尝试登录服务器,但收到错误消息。我正在使用 SBJSON 将请求转换为 JSON,并将其从 JSON 转换回字符串,并且我使用一种方法来进行所有 API 调用。我四处寻找解决方案,但找不到任何解决方案。这是我用来登录的方法:

+ (void)requestFromUrl:(NSString *)urlString withType:(NSString *)type withBody:(NSDictionary *)dict
{
    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:type];

    if(dict)
    {
        NSString *jsonString = [dict JSONRepresentation];
        NSData *jsonData = [jsonString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:true];
        [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:jsonData];
    }

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
     {
         if (error)
         {
             NSLog(@"Error retrieving data: %@", error.description);
         }
         else
         {
             id result = [data JSONValue];
             NSLog(@"Received responce: %@", result);

             [[NSNotificationCenter defaultCenter] postNotificationName:@"ReceivedResponce" object:self userInfo:result];
         }
     }];

有人可以帮我吗?

4

1 回答 1

1

做这个:

    NSData *jsonData = [jsonString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:true];

进入:

    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

看看是否有帮助。JSON 必须是 UTF8 编码,而不是 ASCII。

于 2013-06-23T06:34:36.863 回答