gpt4 book ai didi

objective-c - SBJson 和 NSData

转载 作者:行者123 更新时间:2023-11-28 20:45:05 26 4
gpt4 key购买 nike

我正在从 quizlet.com 获取数据,它适用于简单的代码:

-(void) grabbQuizletWithUrl:(NSURL*)requstURL {

NSString *dataString = [NSString stringWithContentsOfURL:requestURL encoding:NSUTF8StringEncoding error:&error];

NSDictionary *dict = [dataString JSONValue];
}

但我需要使用 NSURLConnection 来启动和停止事件指示器。我在努力

-(void) grabbQuizletWithUrl:(NSURL*)requstURL {

NSURLRequest *quizletRequest = [[NSURLRequest alloc] initWithURL:requestURL];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:quizletRequest
delegate:self];
[connection release];
[quizletRequest release];

}

// and getting data in delegate method:



- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

[self.activityIndicator stopAnimating];

NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

NSDictionary *dict = [dataString JSONValue];

}

我收到这样的消息:

[2377:707] -JSONValue 失败。错误是:意外的输入结束

[2377:707] -JSONValue 失败。错误是: token [.] 的非法开始

[2377:707] -JSONValue 失败。错误是: token [d] 的非法开始

最佳答案

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data; 中,您应该将收到的数据追加到先前存储的数据中,因为您只得到了一部分响应,即:

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// someNSMutableDataIVar is an ivar to store the data in
[someNSMutableDataIVar appendData:data];
}

然后在另一个名为 :- (void)connectionDidFinishLoading:(NSURLConnection *)connection; 的委托(delegate)方法中,您应该处理数据。

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
// the connection finished loading all data, process...
[self.activityIndicator stopAnimating];
NSString *dataString = [[NSString alloc]
initWithData:someNSMutableDataIVar
encoding:NSUTF8StringEncoding];
NSDictionary *dict = [dataString JSONValue];
}

异步 ​​URL 加载系统在 URL Loading System Programming Guide 中有详细描述。来自苹果。

希望对您有所帮助!

关于objective-c - SBJson 和 NSData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6713199/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com