1

データをjsonコードとして表示するphpファイルに多くの非同期リクエストを送信しようとしています。応答で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) は問題なく動作し、出力を取得しましたが、もう 1 つの if 条件 --> if (connection == WikipediaConn) はこの出力 NSLog(@"get from table のみを提供します知恵」); forループをスローしないでください。私のコードに何か問題がありますか?

4

2 に答える 2

3

中括弧を適切に閉じていません...

else - if 条件のブレースを開く前に、if_condition のブレースを閉じる必要があります。

于 2012-11-17T13:50:38.010 に答える
0

関連するコードの一部が欠落しています (NSConnectionDataDelegateメソッド)。responseDataしかし、1 つのインスタンス変数 を使用して両方の戻り値を構築しているように見えます。これは、より高速な応答データのみを取得する場合があることを意味します。NSMutableDataまた、同じオブジェクトで混合データを取得する場合もあります。

于 2012-11-17T13:58:33.180 に答える