0

Iam 从具有两个不同 url 的 viewcontroller 调用用于 json 解析的函数。

- (void)viewDidLoad
{

[super viewDidLoad];

 parseObj=[[json_parser alloc]init];

NSString *group_url=@"http://urlforgroup";
NSString *drug_url =@"http:/urlfordrug";

mConcurrentQueue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
dispatch_async(mConcurrentQueue, ^{tFunct1(group_url,@"group_url");});
dispatch_async(mConcurrentQueue, ^{tFunct1(drug_url,@"drug_url");});
}

void (^tFunct1)(NSString *,NSString *)=^(NSString *base_url,NSString *stat_val){


dispatch_async(dispatch_get_main_queue(), ^(void){

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setObject:[NSString stringWithFormat:@"%@",stat_val] forKey:@"check_url"];
    [parseObj parseUrl:base_url];
});


};

@end

//////////////////////// json parser//////////

 -(void)parseUrl:(NSString *)baseUrl
{


NSLog(@"URL%@",baseUrl);
 NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

NSString *savedValue = [prefs stringForKey:@"check_url"];

NSLog(@"%@",savedValue);// showing different value group_url then drug_url

NSURL *url=[NSURL URLWithString:baseUrl];

NSURLRequest *request=[NSURLRequest requestWithURL:url];

conn=[NSURLConnection connectionWithRequest:request delegate:self];

if(conn)
{

    mdata=[[NSMutableData alloc]init];
}

}


 - (void)connectionDidFinishLoading:(NSURLConnection *)connection

{

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

    // getting an NSString
    NSString *savedValue = [prefs stringForKey:@"check_url"];
    NSDictionary *allData=[NSJSONSerialization JSONObjectWithData:mdata options:0 error:nil];

    NSLog(@"connectionDidFinishLoading %@",savedValue);// showing same value drug url


    if([savedValue isEqualToString:@"grp_url"])
      {


        NSLog(@"some code");

      }
    else
      {
            NSLog(@"some other code"); 


     }



   }

我想根据可验证的“savedValue”的值编写不同的解析代码。但我的问题是在connectionDidFinishLoading内部我对两个url都有相同的值“drug_url”。请帮忙。

4

1 回答 1

0

您还需要实施

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
  [mdata appendData:data];
}

为了这个工作

于 2013-08-13T13:28:09.507 回答