views:

197

answers:

2

Hello,

I'm trying to create a application that will retrieve JSON data from an HTTP request, send it to a the application main controller as a JSON object then from there do further processing with it.

Where I'm stick is actually creating a class that will serve as a JSON class in which will take a URL, grab the data, and return that object.

Alone, im able to make this class work, however I can not get the class to store the object for my main controller to retrieve it.

Because im fairly new to Objective-C itself, my thoughts are that im messing up within my init call:

-initWithURL:(NSString *) value
{
    responseData = [[NSMutableData data] retain];
    NSURL *theURL = [NSURL URLWithString:value];

    NSURLRequest *request = [NSURLRequest requestWithURL:theURL];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];

    return self;
}

The processing of the JSON object takes place here:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [connection release];

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

    NSError *jsonError;
    SBJSON *json = [[SBJSON new] autorelease];
    NSDictionary *parsedJSON = [json objectWithString:responseString error:&jsonError];

    // NSArray object.
    listings = [parsedJSON objectForKey:@"posts"];
    NSEnumerator *enumerator = [listings objectEnumerator];
    NSDictionary* item;

    // to prove that it does work.
    while (item = (NSDictionary*)[enumerator nextObject]) {
        NSLog(@"posts:id = %@", [item objectForKey:@"id"]);
        NSLog(@"posts:address = %@", [item objectForKey:@"address"]);
        NSLog(@"posts:lat = %@", [item objectForKey:@"lat"]);
        NSLog(@"posts:lng = %@", [item objectForKey:@"lng"]);
    }

    [responseString release];

}

Now when calling the object within the main controller I have this bit of code in the viewDidLoad method call:

- (void)viewDidLoad {
    [super viewDidLoad];

    JSON_model *jsonObj = [[JSON_model alloc] initWithURL:@"http://localhost/json/faith_json.php?user=1&format=json"];

    NSEnumerator *enumerator = [[jsonObj listings] objectEnumerator];
    NSDictionary* item;
    //  
    while (item = (NSDictionary*)[enumerator nextObject]) {
        NSLog(@"posts:id = %@", [item objectForKey:@"id"]);
        NSLog(@"posts:address = %@", [item objectForKey:@"address"]);
        NSLog(@"posts:lat = %@", [item objectForKey:@"lat"]);
        NSLog(@"posts:lng = %@", [item objectForKey:@"lng"]);
    }
}
A: 

take a look at TouchJSON project - http://code.google.com/p/touchcode/wiki/TouchJSON

Dwight Kelly
A: 

I think things are happening in the wrong order.

  1. enter viewDidLoad
  2. [JSON_Model initWithURL] is called from viewDidLoad
  3. initWithURL starts fetching data asynchronously.
  4. initWithURL finishes and returns to viewDidLoad.
  5. viewDidLoad continues and displays the empty content of listings

... Time passes whilst the server generates the JSON and returns it.

  1. connectionDidFinishLoading is called once the iPhone recieves the data
  2. connectionDidFinishLoading populates listings with the JSON data.
  3. listings is not access again

the view is never told to refresh once the JSON data has been loaded and parsed.

Twelve47