如何发送async http post - ios

时间:2014-04-10 18:07:57

标签: ios http post nsurlconnection

我的应用程序有点问题。我想以异步方式向服务器发送一些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也不起作用,其他方法也是如此。 任何人都有想法??

2 个答案:

答案 0 :(得分:2)

如果您想逐个使用异步请求,可以这样做:

- (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
    }
  }];
}
希望这对你有所帮助〜谢谢〜

答案 1 :(得分:0)

您的代码对我来说很好。以下是我的想法:

您确定您的第二个NSURLConnection正在创建并发送出去吗? 也许它永远不会被发送。

您是否正在调用sendHTTPRequest:键入:idNegozio:当您的第二个连接仍然被发送时,具有不同类型的方法?
您没有在发送功能的开头检查,以确保您还没有发送连接。也许你的旗帜正在连接中途切换。

你的didFinish方法中的if语句应该与else结合使用。为了防止您在处理'neg'连接后关闭'app'连接,您不会意外地通过并尝试两次处理响应。

此外,除非将NO传递给构造函数中的startImmediately:参数,否则不必在NSURLConnection上显式调用'start'。这不应该引起问题。