简体   繁体   English

发送许多NSURLConnection请求并在connectionDidFinishLoading中获取响应

[英]send many NSURLConnection requests and get response in connectionDidFinishLoading

I'm trying to send many asynchronous request to php file that shows my data as json code and in the response I parse the json so I added the following in ViewDidload: 我正在尝试向php文件发送许多异步请求,将我的数据显示为json代码,并在响应中解析json,因此我在ViewDidload中添加了以下内容:

NSURLRequest *aboutRequest = [self getDataFromServer:@"http://al-awal.com/Rashad/iPhonePhp/about.php"];
aboutConn = [[NSURLConnection alloc] initWithRequest:aboutRequest delegate:self];


NSURLRequest *wisdomRequest = [self getDataFromServer:@"http://al-awal.com/Rashad/iPhonePhp/wisdomtoday.php"];
wisdomConn = [[NSURLConnection alloc] initWithRequest:wisdomRequest delegate:self];

then 然后

-(NSURLRequest *) getDataFromServer:(NSString *)phpUrl{
responseData = [NSMutableData data];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:phpUrl]];

return request;
}

then I added: 然后我补充说:

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

    sqlite3 *database;

    if(sqlite3_open([[rashadDB databasePath] UTF8String], &database) == SQLITE_OK) {

        if (connection == aboutConn) {
            NSLog(@"get from table about");
            SBJsonParser *parser = [[SBJsonParser alloc] init];

            NSString *json_string = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

            NSArray *statuses = [parser objectWithString:json_string error:nil];

            for (NSDictionary *status in statuses)
            {

                NSString *sAboutID = [status objectForKey:@"ID"];
                NSString *sPhone1 = [status objectForKey:@"Phone1"];
                NSString *sPhone2 = [status objectForKey:@"Phone2"];
                NSString *sJawal = [status objectForKey:@"Jawal"];
                NSString *sFax = [status objectForKey:@"Fax"];
                NSString *sWebSite = [status objectForKey:@"WebSite"];
                NSString *sEmail = [status objectForKey:@"Email"];
                NSString *sAddress = [status objectForKey:@"Address"];

                int sAboutID2 = [sAboutID intValue];

                NSLog(@"in server, aboutID = %d, Phone1 = %@, Phone2 = %@, Jawal = %@, Fax = %@, WebSite = %@, Email  = %@, Address = %@",sAboutID2, sPhone1, sPhone2, sJawal, sFax, sWebSite, sEmail, sAddress);

                sqlite3_exec(database, [[NSString stringWithFormat:@"INSERT OR ABORT INTO About VALUES(%d, '%@', '%@', '%@', '%@', '%@', '%@', '%@')",sAboutID2, sPhone1, sPhone2, sJawal, sFax, sWebSite, sEmail, sAddress] UTF8String], NULL, NULL, NULL);

                sqlite3_exec(database, [[NSString stringWithFormat:@"UPDATE About set Phone1 = '%@', Phone2 = '%@', Jawal = '%@', Fax = '%@', WebSite = '%@', Email  = '%@', Address = '%@' Where aboutID = %d", sPhone1, sPhone2, sJawal, sFax, sWebSite, sEmail, sAddress, sAboutID2] UTF8String], NULL, NULL, NULL);

      }  

            } else if (connection == wisdomConn) {
                NSLog(@"get from table wisdoms");

                SBJsonParser *parser = [[SBJsonParser alloc] init];

                NSString *json_string = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

                NSArray *statuses = [parser objectWithString:json_string error:nil];

                for (NSDictionary *status in statuses)
                {
                    NSString *sWisdomTodayID = [status objectForKey:@"ID"];
                    NSString *sContent = [status objectForKey:@"Content"];
                    NSString *sAuthorName = [status objectForKey:@"AuthorName"];
                    NSString *sDateCreate = [status objectForKey:@"DateCreate"];
                    NSString *sEnable = [status objectForKey:@"Enable"];


                    int sWisdomTodayID2 = [sWisdomTodayID intValue];
                    int sEnable2 = [sEnable intValue];

                    NSLog(@"wisdomTodayID = %@, Content = '%@', AuthorName = '%@', DateCreate = '%@', Enable = %@", sWisdomTodayID, sContent, sAuthorName, sDateCreate, sEnable);

                    sqlite3_exec(database, [[NSString stringWithFormat:@"INSERT OR ABORT INTO WisdomToday VALUES(%d, '%@', '%@', '%@', %d)",sWisdomTodayID2, sContent, sAuthorName, sDateCreate, sEnable2] UTF8String], NULL, NULL, NULL);

                    sqlite3_exec(database, [[NSString stringWithFormat:@"UPDATE WisdomToday set Content = '%@', AuthorName = '%@', DateCreate = '%@', Enable = %d  Where wisdomTodayID = %d", sContent, sAuthorName, sDateCreate, sEnable2, sWisdomTodayID2] UTF8String], NULL, NULL, NULL);

                }


            }


        } // sqlite3 open end

        sqlite3_close(database);

    }

the first if condition --> if(connection == aboutConn) works ok with me and I got my output but the other if condition --> if (connection == wisdomConn) only gives me this output NSLog(@"get from table wisdoms"); 第一个if条件 - > if(connection == aboutConn)和我一起正常工作我得到了我的输出但是另一个条件 - > if(connection == wisdomConn)只给我这个输出NSLog(@“从表中获取智慧“); and not go throw the for loop. 而不是去抛出for循环。 Is there are any wrong in my code? 我的代码有什么问题吗?

You are not closing braces properly... 你没有正确关闭牙箍......

Before opening the brace for else - if condition you should close the brace for if_condition.. 在为else打开括号之前 - 如果条件你应该关闭if_condition的括号..

Some of your relevant code is missing (the NSConnectionDataDelegate methods). 缺少一些相关代码( NSConnectionDataDelegate方法)。 But it would seem that you are using one instance variable, responseData , to build up both return values. 但似乎您正在使用一个实例变量responseData来构建两个返回值。 This would mean that some times you would get the faster response data only; 这意味着有时候你会得到更快的响应数据; and other times you would get mixed data in the same NSMutableData object. 有时你会在同一个NSMutableData对象中获得混合数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在connectionDidFinishLoading内部创建一个新的NSURLConnection - Creating a new NSURLConnection inside of connectionDidFinishLoading iPhone-为什么NSURLConnection connectionDidFinishLoading没有运行? - iphone - Why the NSURLConnection connectionDidFinishLoading is not running? 在connectionDidFinishLoading之后收到NSURLConnection数据 - NSURLConnection data received after connectionDidFinishLoading 如何使用NSURLConnection实例传递信息,以便可以从connectionDidFinishLoading获取信息 - How to pass info with NSURLConnection instance so I can get it from connectionDidFinishLoading 使用NSURLConnection发送一系列HTTP请求的最佳方式 - Best way to send a series of HTTP requests with NSURLConnection NSURLConnection和connectionDidFinishLoading在多个函数之间共享 - NSURLConnection and connectionDidFinishLoading sharing between multiple functions NSURLConnection didFailWithError connectionDidFinishLoading同时调用? - NSURLConnection didFailWithError connectionDidFinishLoading called at same time? NSURLConnection响应 - NSURLConnection Response A /同步NSURLConnection:在没有下载数据的情况下获得响应? - A/Synchronous NSURLConnection: get response without download data? iPhone NSURLConnection:connectionDidFinishLoading - 如何将字符串返回给调用方法 - iPhone NSURLConnection: connectionDidFinishLoading - how to return a string to the calling method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM