gpt4 book ai didi

iphone - 检查 NSURLConnection 是否在 iphone 中不成功?

转载 作者:搜寻专家 更新时间:2023-10-30 19:44:19 24 4
gpt4 key购买 nike

-(void)loadRequest:(NSString *)jsonString{
receivedData = [[NSMutableData alloc]init];
urlString = [[NSString alloc]initWithString:[NSString stringWithFormat:kURL]];
urlRequest=[NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
requestInformation =[[NSMutableURLRequest alloc]initWithURL:urlRequest cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];
[requestInformation setValue:khttpValue forHTTPHeaderField:kContentType];
[requestInformation setValue:@"value1" forHTTPHeaderField:@"key1"];

[requestInformation setHTTPMethod:kPost];
jsonData= [[NSString stringWithFormat:@"json=%@",jsonString] dataUsingEncoding:NSUTF8StringEncoding];
[requestInformation setHTTPBody:jsonData];

connection = [[NSURLConnection alloc] initWithRequest:requestInformation delegate:self];
[connection start];
if(connection){
NSLog(@"Connection succesfull");
}
else{
NSLog(@"There is a error in connection");
[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(onFailedToUpload) userInfo:nil repeats:NO];

}
}

如何检查else部分是否被执行。我给出了错误的 URL,调用了 didfailwitherror 方法但没有执行其他部分。

最佳答案

据我所知,总是会创建connection 对象。即使你的网址是错误的。委托(delegate) didFailWithError 方法出现任何错误。您可能需要检查错误并适本地继续。前任。如果超时,您可能想在 didFailWithError 委托(delegate)中重试。对于其他错误类型,处理方式不同。

如果你想在将它传递给 NSURLConnection 之前处理损坏或错误的 url,那么你需要自己做。

以下是在使用 NSURLConnection 时对您有用的委托(delegate) -

- (void)connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *)response
{
NSLog("@Resp received");
return;
}

- (void)connection:(NSURLConnection *)connection
didReceiveData:(NSData *)data
{
NSLog("@Data received");
return
}

- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error
{
NSLog("@ERROR: Achtung !: %@",[error localizedDescription]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH , 0),, ^{
NSLog(@"FinishedLoading: In bg thread, do something with data here");

dispatch_async( dispatch_get_main_queue(), ^{
NSLog(@"FinishedLoading: In Main thread, access the UI here");
});
});
}

关于iphone - 检查 NSURLConnection 是否在 iphone 中不成功?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12346359/

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