0

こんにちは、オンデマンドでデータを読み込もうとしています。データが初めてロードされた後、次のメソッドを呼び出します。

-(void)carregaDados2
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;


    NSURL *url2 = [[NSURL alloc] initWithString:[@"http://localhost:3000/json.aspx?ind=12&tot=12" stringByAddingPercentEscapesUsingEncoding:NSISOLatin1StringEncoding]];

    NSURLRequest *request = [NSURLRequest requestWithURL:url2];
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSError *jsonParsingError = nil;

    NSMutableData *data2;
    data2 = [[NSMutableData alloc] init];
    [data2 appendData:response];
    NSMutableData *dt = [[NSMutableData alloc] init];
    [dt appendData:data];
    [dt appendData:data2];
    news = [NSJSONSerialization JSONObjectWithData:dt options:nil error:nil];  
    [tableViewjson reloadData];
    NSLog(@"Erro: %@", jsonParsingError);
}

しかし、私のテーブルビューは空白です。

私は何を間違っていますか?


他に何をすべきか本当にわかりません。

コードを変更したので、UITableView に NSMutableArray の 2 番目のインデックスを配置できません

-(void)carregaDados2
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;


    NSURL *url2 = [[NSURL alloc] initWithString:[@"http://localhost:3000/json.aspx?ind=12&tot=12" stringByAddingPercentEscapesUsingEncoding:NSISOLatin1StringEncoding]];

    NSURLRequest *request = [NSURLRequest requestWithURL:url2];
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSError *jsonParsingError = nil;

    NSMutableData *myData = [[NSMutableData alloc]init];
    [myData appendData:response];


    NSMutableArray *news2;

    news2 = [NSJSONSerialization JSONObjectWithData:myData options:nil error:&jsonParsingError];
    NSLog(@"LOG: %@", news2);
}
4

1 に答える 1

2

見た目からすると、2 つの JSON 応答バッファーを一緒に粉砕し、それらを 1 つの JSON メッセージとして解析しようとしています。

[dt appendData:data];
[dt appendData:data2];
news = [NSJSONSerialization JSONObjectWithData:dt options:nil error:nil];  

これは機能しません。

たとえば、if datais[{"x"="a","y"="b"}]data2is [{"x"="c","y"="d"}]thendtの値[{"x"="a","y"="b"}][{"x"="c","y"="d"}]は無効な JSON メッセージになります。

JSON メッセージを NSArrays に解析し、2 つの配列を別々に結合することをお勧めします。


2 つの配列を結合することは、基本的な NSArray/NSMutableArray 操作です。

NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSError *error = nil;

NSArray *updatedNews = [NSJSONSerialization JSONObjectWithData:response options:nil error:&error];

// If news is of type NSArray…
news = [news arrayByAddingObjectsFromArray:updatedNews];

// If news is of type NSMutableArray…
[news addObjectsFromArray:updatedNews];

[tableViewjson reloadData];
于 2013-05-29T14:06:16.127 に答える