1

コードで問題が発生していますが、その理由がわかりません。JSONエラーというエラーが表示されるだけです。UITableView は何も満たされません。私はiOSの経験があまりないので、助けていただければ幸いです。

//
//  ViewController.m
//  Westmount Procrastinator
//
//  Created by Saleem on 10/25/13.
//  Copyright (c) 2013 Saleem Al-Zanoon. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (strong, nonatomic) IBOutlet UIWebView *webView;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *fullURL = @"********";
    NSURL *url2 = [NSURL URLWithString:fullURL];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url2];
    [_webView loadRequest:requestObj];

    self.title = @"News";

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    NSURL *url = [NSURL URLWithString:@"****************"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [NSURLConnection connectionWithRequest:request delegate:self];    //  NSString * urlString = [NSString stringWithFormat:@"http://salespharma.net/westmount/get_all_products.php"];
  //  NSURL * url = [NSURL URLWithString:urlString];
 //   NSData * data = [NSData dataWithContentsOfURL:url];
 //   NSError * error;
//    NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
//    NSLog(@"%@",json);
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    data = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
{
    [data appendData:theData];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    NSArray *responseDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:NULL];
    //news = [responseDict objectAtIndex:0];
    // [mainTableView reloadData];
    if ([responseDict isKindOfClass:[NSArray class]]) {
        news = responseDict;
        [mainTableView reloadData];
    } else {
        // Looks like here is some part of the problem but I don't know why.
        NSLog(@"JSON Error.");

        UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Could not contact server!" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
        [errorView show];

    }
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"The download could not complete - please make sure you're connected to either 3G or Wi-Fi." delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
    [errorView show];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

- (int)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [news count];
}

NSString *_getString(id obj)
{
    return [obj isKindOfClass:[NSString class]] ? obj : nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
    }

    cell.textLabel.text = _getString([[news objectAtIndex:indexPath.row] objectForKey:@"Issue"]);
    cell.detailTextLabel.text = _getString([[news objectAtIndex:indexPath.row] objectForKey:@"Name"]);

    return cell;
}
   @end

JSON がインターネット上でどのように見えるか:

    {

   "Issues":[
      {
         "Issue":"2",
         "Link":"google.com",
         "Name":"Ios Test"
      },
      {
         "Issue":"3",
         "Link":"Yahoo",
         "Name":"iOS test 2"
      }
   ],
   "success":1
}

編集:私の質問が明確でなくて申し訳ありません。アプリはクラッシュしませんが、これを表示するログのデータベースにデータをロードできません:

2013-10-26 10:26:41.670 Westmount Procrastinator[2490:70b] JSON Error.
2013-10-26 10:26:41.671 Westmount Procrastinator[2490:70b] Server Data: 
{"Issues":[{"Issue":"2","Link":"google.com","Name":"Ios Test"}],"success":1}

データベースに接続するアプリケーションの目標は、新聞の問題のリストをダウンロードし、リスト ビューにリストします。次に、ユーザーが問題をクリックしてダウンロードできるようにします。

編集説明を助けるためにJSONにさらに追加しました。

4

2 に答える 2

3

サンプルの JSON 構造からは、NSArray ではないようです。代わりに NSDictionary です。したがって、JSON データを解析している間は、NSArray ではなく NSDictionary に保存してください。また、後でIF条件を変更してください。

重要なのは、テーブルビューが NSDictionaries の NSArray からデータを読み取っている場合、この NSDictionary を NSArray に入れてテーブル ビューに渡すことです。また、それに応じて処理する必要がある複数の辞書である場合の出力をサーバー側から確認してください。したがって、基本的には、ここで誘導する必要がある行がさらにいくつかあります。または、すべての場合にデータプロバイダー (サーバー側) に NSArray を送信するように依頼する必要があります。

NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:NULL];
if ([responseDict isKindOfClass:[NSDictionary class]]) {
    NSArray *tableArray = [NSArray arrayWithArray:responseDict[@"Issues"]];
}

次に、tableArray を使用してテーブルにデータを入力します。

于 2013-10-26T17:29:55.397 に答える
0

Issues はディクショナリの配列であるため、 でディクショナリを要求し、indexpath.rowを使用objectForKeyしてそのディクショナリから適切な値を取得する必要があります。

    NSDictionary *myDict = @{@"Issues": @[@{@"Issue": @"2",
                                            @"Link": @"google.com",
                                            @"Name": @"Ios Test"},
                                          @{@"Issue": @"3",
                                            @"Link": @"Yahoo",
                                            @"Name": @"iOS test 2"}],
                             @"success": @"1"};

    NSArray *issues = [myDict objectForKey:@"Issues"];

    [issues enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        NSLog(@"Issue: %@ Link: %@ Name: %@", [obj objectForKey:@"Issue"], [obj objectForKey:@"Link"], [obj objectForKey:@"Name"]);
    }];

戻ります:

2013-10-26 16:42:43.572 Jsontest[43803:303] Issue: 2 Link: google.com Name: Ios Test
2013-10-26 16:42:43.573 Jsontest[43803:303] Issue: 3 Link: Yahoo Name: iOS test 2
于 2013-10-26T20:43:18.907 に答える