0

我有 2 个单独的 NSURLConnection。

NSURLConnection * connection_users;
NSURLConnection * connection_cards;

然后我用参数等创建了数据,我完成了:

connection_users = [[NSURLConnection alloc] initWithRequest: url_request_users delegate: self startImmediately: YES];

在委托方法中:

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

我检查了连接是否适用于 connection_users:

if (connection == connection_users) / / do something as an example:
NSDictionary * json_response = [NSJSONSerialization JSONObjectWithData: data options: kNilOptions error: & error];

使用来自该方法的“数据”。

在关闭“if”之前,我创建了到“connection_cards”的下一个连接,做同样的事情

在“if”之外,但在相同的方法中,我对“connection_cards”做了另一个“if”,并对 JSONObjectWithData 做同样的事情。

只有来自该方法的“数据”始终是第一个连接的。发生了什么不同的事情?对于第二个连接,您应该收到相应的“数据”。在开始第二个连接之前已经取消了第一个连接以查看是否已解决,但没有。

如何为第二次连接获取正确的“数据”?

PS:如果您需要更多代码,请告诉我。

编辑:

正如韦恩所问

url_request_users               = [[NSMutableURLRequest alloc] init];
NSMutableString *post_users     = [[NSMutableString alloc] init];
[post_users appendFormat:@"%@", [NSString stringWithFormat:@"email=%@&senha=%@",
                                             [[alert textFieldAtIndex:0] text],
                                             senha_md5]];

[url_request_users setURL:[NSURL URLWithString:WBS_USERS_RECOVER]];
[url_request_users addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[url_request_users setHTTPMethod:@"POST"];
[url_request_users setHTTPBody:[post_users dataUsingEncoding:NSUTF8StringEncoding]];

connection_users   = [[NSURLConnection alloc] initWithRequest:url_request_users delegate:self startImmediately:YES];
4

1 回答 1

1

对于 n 个不同的连接,您将需要 n 个不同NSMutableData的,其中包含相关的结果NSURLConnection。您问题的基本示例;

NSMutableData *data_users;
NSMutableData *data_cards;

比你的 didRecieveData;

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    if (connection == connection_users) {
        [data_users appendData:data];
    } else if ( connection == connection_cards) {
        [data_cards appendData:data];
    }  
}

这样您就可以分别跟踪您的数据和连接。请记住在连接结束时清除剩余的数据

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    if (connection == connection_users) {
        // use data from data_users
        NSDictionary * json_response = [NSJSONSerialization JSONObjectWithData:[data_users copy] options: kNilOptions error: & error];
        data_users = [[NSMutableData alloc] init]; // clear data users
    }
    // do the same for cards connection
}

最后要做的是在调用这个函数之前分配你的数据;

url_request_users               = [[NSMutableURLRequest alloc] init];
NSMutableString *post_users     = [[NSMutableString alloc] init];
[post_users appendFormat:@"%@", [NSString stringWithFormat:@"email=%@&senha=%@",
                                             [[alert textFieldAtIndex:0] text],
                                             senha_md5]];

[url_request_users setURL:[NSURL URLWithString:WBS_USERS_RECOVER]];
[url_request_users addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[url_request_users setHTTPMethod:@"POST"];
[url_request_users setHTTPBody:[post_users dataUsingEncoding:NSUTF8StringEncoding]];

data_users = [[NSMutableData alloc] init]; // add this line in your code 
connection_users   = [[NSURLConnection alloc] initWithRequest:url_request_users delegate:self startImmediately:YES];
于 2013-05-24T22:44:43.963 回答