我知道只获取Json的数据。但我需要如何获取两个json url的数据。 我有两个Urls和两个MutableArrays。每次我只能处理一个这样的URL: -
NSURL * url=[NSURL URLWithString:@" url"];
NSURLRequest *req=[NSURLRequest requestWithURL:url];
connection=[NSURLConnection connectionWithRequest:req delegate:self];
if(connection)
{
NSLog(@"Connected");
webData=[[NSMutableData alloc]init];
NSLog(@"Connected2");
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
return arrayfirst.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
return arrayfirst[row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
one.text = arrayfirst[row];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSMutableArray *al=[NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];
NSLog(@"the array is %@",al);
for(NSArray *arr in al)
{
[arrayfirst addObject:[arr objectAtIndex:1]];
}
}
请告诉我如何处理两个Json网址。我有两个网址BusinessCategory
和BusinessLocation
我有两个MutableArrays
arrayfirst
和arraysecond
。我知道BusinessCategory
数据传递给arrayfirst
。但我不知道如何处理。
答案 0 :(得分:0)
您必须创建两个NSURL连接并在委托中处理响应。
So, in your header file you can have:
NSURLConnection *conn1;
NSURLConnection *conn2;
NSMutableArray *arrayFirst;
NSMutableArray *arraySecond;
In the implementation file:
//Connection 1
NSURL *url1=[NSURL URLWithString:@"yourjsonurl1"];
NSURLRequest *request1 = [NSURLRequest requestWithURL:url1];
conn1=[[NSURLConnection alloc] initWithRequest:request1 delegate:self];
//Connection 2
NSURL *url2=[NSURL URLWithString:@"yourjsonurl2"];
NSURLRequest *request2 = [NSURLRequest requestWithURL:url2];
conn2=[[NSURLConnection alloc] initWithRequest:request2 delegate:self];
//In your delegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
if (connection==conn1) {
arrayFirst = [[NSMutableData alloc]init];
}
else if (connection==conn2){
arraySecond = [[NSMutableData alloc]init];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
if (connection==conn1) {
[arrayFirst appendData:data];
}
else if (connection==conn2){
[arraySecond appendData:data];
}
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
if (connection==conn1) {
NSLog(@"SOMETHING WENT WRONG WITH URL1");
}
else if (connection==conn2){
NSLog(@"SOMETHING WENT WRONG WITH URL2");
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
if (connection==conn1) {
NSLog(@"DONE WITH URL1");
}
else if (connection==conn2){
NSLog(@"DONE WITH URL2");
}
}
更短(也许更好)的方法是在异步操作中运行它:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *result = [NSData dataWithContentsOfURL:"yourURL"];
id jsonResult;
if ([result length] != 0) { jsonResult = [NSJSONSerialization JSONObjectWithData:result options:0 error:nil];}
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"done!");
});
});