【问题标题】:Content-Type JSON SIGABRT Error内容类型 JSON SIGABRT 错误
【发布时间】:2013-02-13 19:36:32
【问题描述】:

我正在尝试解析 JSON,并收到 SIGABRT 错误:reason: '[<NSURLRequest 0x7e7e970> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key Content-Type.

我的代码是:

self.responseData = [NSMutableData data];
NSURLRequest *request = [NSURLRequest requestWithURL:
                         [NSURL URLWithString:@"http://dmsfw01.dev.com:8082/G.FQI/GVOLFQI.svc/Search(v)?Start=1&Count=10"]];

NSString *contentType = @"application/json";
[request setValue:contentType forKey:@"Content-Type"];

[[NSURLConnection alloc] initWithRequest:request delegate:self];

我应该怎么做才能设置 Content-Type?

【问题讨论】:

    标签: ios objective-c json nsurlrequest


    【解决方案1】:

    您收到错误消息,因为您正在使用 Key Value Coding 方法尝试根据请求设置名称为 Content-Type 的属性。您也不能修改NSURLRequest,因为它是不可变的。请改用NSMutableURLRequest,然后调用- (void)setValue:(NSString *)value forHTTPHeaderField:(NSString *)field

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:
                                    [NSURL URLWithString:@"http://sw730voldmsfw01.vol1dev.com:8082/GVOL.FQI/GVOLFQI.svc/Search(visa)?Start=1&Count=10"]];
    
    NSString *contentType = @"application/json";
    [request setValue:contentType forHTTPHeaderField:@"Content-Type"];
    

    【讨论】:

    • ...另外,使用setValue:forHTTPHeaderField 方法,而不是setValue:forKey
    猜你喜欢
    • 2021-07-27
    • 2012-11-01
    • 2022-01-18
    • 2015-12-28
    • 2017-08-15
    • 2021-04-04
    • 1970-01-01
    • 1970-01-01
    • 2022-07-20
    相关资源
    最近更新 更多