我正在尝试向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];
然后
-(NSURLRequest *) getDataFromServer:(NSString *)phpUrl{
responseData = [NSMutableData data];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:phpUrl]];
return request;
}
然后我补充说:
- (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);
}
第一个if条件 - > if(connection == aboutConn)和我一起正常工作,我得到了我的输出但是另一个条件 - > if(connection == wisdomConn)只给我这个输出NSLog(@“从表智慧中获取”);而不是去抛出for循环。 我的代码有什么问题吗?
答案 0 :(得分:3)
你没有正确关闭牙箍......
在打开else的括号之前 - 如果条件你应该关闭if_condition的括号..
答案 1 :(得分:0)
缺少部分相关代码(NSConnectionDataDelegate
方法)。但似乎您正在使用一个实例变量responseData
来构建两个返回值。这意味着有时候你会得到更快的响应数据;有时你会在同一个NSMutableData
对象中获得混合数据。