0

我正在使用一个以 json 对象作为参数的 web 服务。这是我的代码:

    -(void)createHttpHeaderRequest {

        NSString *x = @"{\"GetVehicleInventory\": {\"ApplicationArea\": {\"Sender\": {\"ComponentID\":}}}" (something like that)

        NSString *sample = [NSString stringWithFormat:@"https://trialservice.checkitout?XML_INPUT=%@",x];
 NSString * final = (NSString *) CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)sampleReq, NULL, CFSTR(":/?#[]@!$&'()*+,;=\""), kCFStringEncodingUTF8);
 NSMutableRequest *request = [NSMutableREquest requestWithURL:[NSURL URLWithString:final]];
    NSURLConnection * theConnection = [[NSURLConnection alloc]initWithRequest:theRequest delegate:self];
        if (theConnection) {
            NSLog(@"Service hit");
        }
    }

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
        NSError * error;
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
        NSArray * categories = [dict objectForKey:@"category"];
        [self.delegate giveControlBackToController:categories];

    }

当我尝试 NSLog 示例时,它会为我提供完整的 URL,粘贴到浏览器中会返回结果,但是当我在请求上调用 NSLog 时,它显示为 null,此后什么也没有发生。控件永远不会转到其 NSURLConnection 委托方法。

4

2 回答 2

1

诀窍是大多数浏览器会自动转义 URL,而 NSURL 不会。您需要手动完成;看看CFURLCreateStringByAddingPercentEscapes函数。

于 2012-07-25T18:54:29.163 回答
0

这可能应该是评论而不是答案,但不幸的是,您无法在评论中格式化代码。将以下方法添加到您的委托,并查看它是否被调用。如果是这样,请告诉我们响应是什么。

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
    NSLog(@"Connection response code: %d", httpResponse.statusCode);
}
于 2012-07-25T20:13:07.990 回答