Iam new to iphone development, i want to get data from the web service using JSON parsing here is the code
-(void)loadDataSource
{
NSString *URLPath = [NSString stringWithFormat:@"https://ajax.googleapis.com/ajax/services/feed/find?v=1.0&q=Official%20Google%20Blogs"];
NSURL *URL = [NSURL URLWithString:URLPath];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode];
if (!error)// && responseCode == 200)
{
id res = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
if (res && [res isKindOfClass:[NSDictionary class]])
{
self.dict=[res objectForKey:@"responseData"];
self.items = [self.dict objectForKey:@"entries"];
[self dataSourceDidLoad];
}
else
{
[self dataSourceDidError];
}
}
else
{
[self dataSourceDidError];
}
}];
}
when i run this code it displays nothing and code for collection view at index is
- (PSCollectionViewCell *)collectionView:(PSCollectionView *)collectionView viewAtIndex:(NSInteger)index
{
NSDictionary *item = [self.items objectAtIndex:index];
PSBroView *v = (PSBroView *)[self.collectionView dequeueReusableView];
if (!v)
{
v = [[PSBroView alloc] initWithFrame:CGRectZero];
}
[v fillViewWithObject:item];
return v;
}
below the code for fillViewWithObject
- (void)fillViewWithObject:(id)object
{
[super fillViewWithObject:object];
self.captionLabel.text = [object objectForKey:@"title"];
}
You apparently didn’t check your error, because when I run this I get “bad URL” as the error. I also get a compiler warning, “more % conversions than arguments”. That’s because of the % in your url string. You shouldn’t be using stringWithFormat — just pass the literal string, and it should work:
I see this error (or just wasted code) a lot. You shouldn’t use stringWithFormat unless you are supplying a format string and arguments.