gpt4 book ai didi

ios - 实现 NSURLConnection 委托(delegate)

转载 作者:行者123 更新时间:2023-11-29 03:04:33 24 4
gpt4 key购买 nike

我是网络服务的新手。我正在尝试实现此协议(protocol),但我有一些问题:

    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"connection didReceiveResponse");
_responseData = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"connection didReceiveData");
[self.responseData appendData:data];
}

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
willCacheResponse:(NSCachedURLResponse*)cachedResponse
{
// Retourne nil pour indiquer qu'il n'est pas nécessaire de stocker les réponses en cache pour cette connexion
return nil;
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{

// Si on a reçu des données, on peut parser
if(self.responseData.length > 0)
{
NSLog(@"connectionDidFinishLoading lenght data = %i", self.responseData.length);
NSError **parseError = nil;

self.myDictionnaryData = [NSJSONSerialization JSONObjectWithData:self.responseData
options:0
error:parseError];

if(parseError != nil)
NSLog(@"Erreur lors du parse des données");
}

self.finished = YES;

}

// Appelé s'il y a une erreur related to the URL connection handling / server response.
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
self.finished = YES;
NSLog(@"connection didFailWithError");
}

但我有一个问题:第一个重定向不起作用..

-(IBAction)submitForm:(id)sender
{
self.finished = NO;


// On met l'url avec les variables sous forme de chaine de caractere
NSString *post =[NSString stringWithFormat:@"login=%@&pwd=%@&regid=%@&platform=%@&version=%@",self.identifying,self.password,token,platform,systemVersion];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];

NSMutableURLRequest *request = [ [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:URL_LOGIN_API]] autorelease];
[request setHTTPMethod:@"POST"]; // de type post
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

[NSURLConnection connectionWithRequest:request delegate:self];

while(!self.finished) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}

[NSURLConnection connectionWithRequest:request delegate:self];

这是我的连接。当我第一次重定向时,我字典中的所有对象都是空的,当我重试时,没问题.. 似乎该方法是在请求之后调用的

编辑:如果我添加类似的内容,它会起作用:

while(!self.finished) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"connectionDidFinishLoading");
NSError **parseError = nil;
self.myDictionnaryData = [NSJSONSerialization JSONObjectWithData:self.responseData
options:0
error:parseError];

if(parseError != nil)
{
NSLog(@"Erreur lors du parse des données");
}

self.finished = YES;
}

最佳答案

对于异步请求,您可以使用。

NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:url_string]]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue currentQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
NSString *returnString1 = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSMutableDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:[returnString1 dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:nil];}];

关于ios - 实现 NSURLConnection 委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22932907/

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