如何在 iOS 中获取两个 json url 的数据
Posted
技术标签:
【中文标题】如何在 iOS 中获取两个 json url 的数据【英文标题】:how to get data for Two json urls in iOS 【发布时间】:2014-07-24 07:31:03 【问题描述】:我只知道获取 Json 的数据。但我需要如何获取两个 json url 的数据。 我有两个 Url 和两个 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 url。我有两个网址 BusinessCategory
和 BusinessLocation
我有两个 MutableArrays
arrayfirst
和 arraysecond
。我知道BusinessCategory
数据传递给arrayfirst
。但是不知道怎么处理。
【问题讨论】:
请说明您想问什么?更具体。 @Xeieshan 我有两个 UITextFields,我有 UIpickerview 和两个 json urls.json 数据传递给基于 textFileds 的 uipickerview 所以请给我任何想法 【参考方案1】:您必须创建两个 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!");
);
);
【讨论】:
我按照你说的尝试过,但是在 connectionDidFinishLoading 中我尝试过这样 if (connection==con1) NSLog(@"DONE WITH URL1"); NSMutableArray *al=[NSJSONSerialization JSONObjectWithData:webData options:0 error:nil]; NSLog(@"第一个数组是 %@",al); else if (connection==con2) NSLog(@"DONE WITH URL2"); NSMutableArray *al2=[NSJSONSerialization JSONObjectWithData:webData2 options:0 error:nil]; NSLog(@"第二个数组是 %@",al2);但它只显示第二个数组第一个数组数据不显示所以请给出任何想法以上是关于如何在 iOS 中获取两个 json url 的数据的主要内容,如果未能解决你的问题,请参考以下文章