我有两个问题:
1.我想在我的应用中从json加载信息,现在'NSLog(@"Array: %@", self.news);' 没有显示任何东西,但如果我把它放在 '(void)connectionDidFinishLoading:(NSURLConnection *)connection' 中它可以工作,你能告诉我为什么吗?
//making request query string
NSString *requestUrl = [NSString
stringWithFormat:@"%@jsons/json.php?go=product_info&latitude=%g&longitude=%g&identifire=%@&pid=%ld&externalIPAddress=%@&localIPAddress=%@",
BASE_URL,
coordinate.latitude,
coordinate.longitude,
uniqueIdentifier,
(long)self.productId,
[self getIPAddress],
[self getLocalIPAddress]
];
NSURL *url=[NSURL URLWithString:requestUrl];
NSURLRequest *request= [NSURLRequest requestWithURL:url];
NSURLConnection *c=[[NSURLConnection alloc] initWithRequest:request delegate:self];
NSLog(@"Array: %@", self.news);
}
//=========================
-(void)connection: (NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
self.jsonData= [[NSMutableData alloc] init];
}
-(void)connection: (NSURLConnection *)connection didReceiveData:(NSData *)theData{
[self.jsonData appendData:theData];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
self.news=[NSJSONSerialization JSONObjectWithData:self.jsonData options:nil error:nil];
}
-(void)connection: (NSURLConnection *)connection didFailWithError:(NSError *)error{
UIAlertView *errorView=[[UIAlertView alloc] initWithTitle:@"Error" message:@"download could not be compelete" delegate:nil cancelButtonTitle:@"Dissmiss" otherButtonTitles:nil, nil];
[errorView show];
}
2.对于这行代码“self.news=[NSJSONSerialization JSONObjectWithData:self.jsonData options:nil error:零];'
self.news 是一个数组,我将其更改为字典,但我收到了相同的警告消息。