【问题标题】:NSURLConnection response not completeNSURLConnection 响应未完成
【发布时间】:2013-04-13 03:23:33
【问题描述】:

我正在使用 NSURLConnection 调用请求 JSON 数据的服务器。

出于某种原因,我得到了部分回复。如果我通过浏览器点击网址,则响应正确。奇怪的是它只在某个时候发生。所以我很难调试这个问题。

然后由于响应不完整,我收到以下错误: Error Domain=NSCocoaErrorDomain Code=3840“操作无法完成。(Cocoa 错误 3840。)”(字符 0 周围的值无效。) UserInfo=0xa4634a0 {NSDebugDescription=字符 0 周围的值无效。} { NSDebugDescription = "字符 0 周围的值无效。"; }

我想这也可能是服务器本身的问题。这是我的代码:

-(void) getShareHistory:(NSString *)range paging:(NSInteger *)page{
     NSString *post = [NSString stringWithFormat:@"range=%@&paging=%@",
                      range,
                      [NSString stringWithFormat:@"%ld",(long)page]];
     NSString *url = [NSString stringWithFormat:@"http://www/domai.com/handle_share_links.php?action=history"];
     NSData *post_data = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
     NSString *postLength = [NSString stringWithFormat:@"%d", [post_data length]];
     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
     [request setCachePolicy:NSURLRequestUseProtocolCachePolicy];
     [request setURL:[NSURL URLWithString:url]];
     [request setHTTPMethod:@"POST"];
     [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
     [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
     [request setHTTPBody:post_data];

     self.shareHistoryConn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)response{
     NSString *strData = [[NSString alloc]initWithData:response encoding:NSASCIIStringEncoding];
     NSLog(@"response %@",strData);
     NSError *jsonParsingError = nil;
     if(connection == self.shareHistoryConn)
     {
        NSArray *data = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingAllowFragments error:&jsonParsingError];
    if(!jsonParsingError)
    {
        [[self delegate] onGetShareHistorySuccess:data];
    }else{
        [[self delegate] onGetShareHistoryFailed:jsonParsingError];
    }
}

提前致谢。

【问题讨论】:

    标签: xcode nsurlconnection nsjsonserialization


    【解决方案1】:

    您看到的是正常行为。 didReceiveData 可以被调用任意次数。您可以继续积累数据,直到获得connectionDidFinishLoading

    标准的委托结构是这样的:

    - (void) connection:(NSURLConnection *)connection
            didReceiveResponse:(NSURLResponse *)response {
        // connection is starting, clear buffer
        [self.receivedData setLength:0];
    }
    
    - (void) connection:(NSURLConnection *)connection
            didReceiveData:(NSData *)data {
        // data is arriving, add it to the buffer
        [self.receivedData appendData:data];
    }
    
    - (void)connection:(NSURLConnection*)connection
            didFailWithError:(NSError *)error {
        // something went wrong, clean up interface as needed
    }
    
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
        // all done, we are ready to rock and roll
        // do something with self.receivedData
    }
    

    始终实现所有四个委托方法。

    【讨论】:

    • 欲了解更多信息,请参阅我书中的相关部分:apeth.com/iOSBook/ch37.html#_http_requests
    • 这正是问题所在。再次感谢。我会在 didReceiveResponse self.receivedData = [[NSMutableData alloc] initWithCapacity:0] 中将对象的初始化添加到您的答案中。
    • 不错的主意,谢谢。如果你要这样做,顺便说一句,你不妨直接说[NSMutableData new]
    • 我已经调试了一段时间,并且即将实现一个复杂的 hack 来尝试让它工作。我希望这被记录得更好一些。但是非常感谢马特!这正是我的问题。
    猜你喜欢
    • 1970-01-01
    • 2018-10-08
    • 1970-01-01
    • 1970-01-01
    • 2012-03-10
    • 1970-01-01
    • 2023-03-23
    • 2019-10-14
    • 2023-04-10
    相关资源
    最近更新 更多