gpt4 book ai didi

ios - 如何发送异步 http post - ios

转载 作者:行者123 更新时间:2023-11-28 22:10:40 25 4
gpt4 key购买 nike

我的应用程序有点问题。我想向服务器异步发送一些 http 请求。我创建了这个方法:

- (void)sendHTTPRequest:(NSString *)urlString type:(NSString *)type idNegozio:(NSNumber *)idNegozio {

self.negozi = [[NSMutableArray alloc] init];
NSData *jsonData;
NSString *jsonString;

if ([type isEqualToString:@"shops"]) {

self.reqNeg = YES;
self.reqApp = NO;

...

    jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:0 error:nil];
jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];



else if ([type isEqualToString:@"appointments"])
{
[self.loadingIconApp startAnimating];

self.reqNeg = NO;
self.reqApp = YES;

...

            jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:0 error:nil];
jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];

NSString *requestString = [NSString stringWithFormat:urlString];
NSURL *url = [NSURL URLWithString:requestString];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody: jsonData];

NSURLConnection * conn = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];

[conn start];

我使用这种方法进行连接:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
self.responseData = [[NSMutableData alloc] init];
}

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

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
willCacheResponse:(NSCachedURLResponse*)cachedResponse {
return nil;
}

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

if (self.reqNeg == YES) {
//here use the responseData for my first http request
}

if (self.reqApp == YES) {

//here use the responseData for second http request
}


}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

}

但以这种方式只有第一个连接有效,我可以使用 responseData。同时,如果我尝试发送其他 http 请求,方法 connectionDidFinishLoading 不起作用,其他方法也不起作用。有人有想法吗??

最佳答案

如果你想一个一个地使用异步请求,你可以这样做:

- (void)request1 {
NSString *requestString = @"your url here";
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:[[NSURLRequest alloc]initWithURL:[NSURL URLWithString: requestString]]
queue:queue
completionHandler:
^(NSURLResponse *response, NSData *data, NSError *error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if (!error && httpResponse.statusCode >= 200 && httpResponse.statusCode <300) {
// call the request2 here which is similar to request 1
// your request2 method here
}
}];
}

希望对你有帮助~谢谢~

关于ios - 如何发送异步 http post - ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22995726/

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