1
  • 这是我调用 Web 服务的代码。

    (void)viewDidLoad

    { [超级 viewDidLoad];

    //Make this controller the delegate for the location manager.self.mapview.delegate = self; 
    
    [self.mapview setShowsUserLocation:YES];
    
    //Instantiate a location object.
    locationManager = [[CLLocationManager alloc] init];
    
    [locationManager setDelegate:self];
    
    //Set some parameters for the location object.
    [locationManager setDistanceFilter:kCLDistanceFilterNone];
    
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    
    
    firstLaunch=YES;
    

    NSURL*url = [NSString stringWithFormat:@"http://198.71.54.13/service.php?CategoryID=%@&SubCategoryID=%@&latitude=%@&longitude=%@&radius=100000type=list",_cat_id,_subcat_id,currentCentre.latitude,currentCentre.longitude,app_var.radius];

    NSURL *googleRequestURL=[NSURL URLWithString:url];
    
        // Retrieve the results of the URL.
    
    
        dispatch_async(kBgQueue, ^{
            NSData* data = [NSData dataWithContentsOfURL: googleRequestURL];
            // NSValue* data12 = [NSValue dataWithContentsOfURL: googleRequestURL];
            NSLog(@"%@ data",data);
    
    
            [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
    
    
        });
    
    }
    
  • 在这里我正在获取 Json 数据

    -(void)fetchedData:(NSData *)responseData 
     {
        NSError* error;
    NSDictionary* json = [NSJSONSerialization 
                                  JSONObjectWithData:responseData                               
                                  options:kNilOptions 
                                  error:&error];
    
     NSArray* places = [json objectForKey:@"business"]; 
    [self plotPositions:places];
    }
    
    
                                             **OUTPUTS**
    
  • 当没有来自 json 的数据时,我在控制台中收到以下错误:

    <5b5d0a> responseData -[__NSArrayI objectForKey:]: unrecognized selector sent to instance 0x910e490 2012-10-19 15:17:56.242 SESpringBoardDemo[1731:13a03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI objectForKey:]: unrecognized selector sent to instance 0x910e490'

  • 当有数据并且我得到这样的响应时,如何检查并避免应用程序 <5b5d0a> 响应数据崩溃?

<7b226275 73696e65 7373223a 5b7b2243 6c69656e 744e616d 65223a22 5061726d 696c6c6f 6e222c22 57656253 69746522 3a226162 63642e63 6f6d222c 22416464 72657373 31223a22 20426f75 6c657661 72642042 72756e6f 20437275 7a222c22 41646472 65737332 223a2223 33323022 2c225a69 70223a22 39343922 2c224c61 74697475 6465223a 2231382e 34343437 222c224c 6f6e6769 74756465 223a222d 36362e32 35313822 2c22436f 6e746163 74506572 736f6e22 3a22526f 62657274 6f204669 67756572 6f222c22 50686f6e 654e756d 62657222 3a223738 372d3235 312d3039 3435222c 224d6f62 696c654e 756d6265 72223a22 3738372d 3334302d 38393337 222c2248 61735072 6f6d6f22 3a225922 2c224369 7479223a 22546f61 2042616a 61222c22 53746174 65223a22 50756572 746f2052 69636f22 2c224275 73696e65 73734944 223a2235 227d2c7b 22436c69 656e744e 616d6522 3a225069 7a7a6120 48757422 2c225765 62536974 65223a22 7777772e 6162632e 636f6d22 2c224164 64726573 7331223a 22524d56 20494920 53746167 65222c22 41646472 65737332 223a2220 53616461 73686976 616e6167 61722022 2c225a69 70223a22 35363030 3738222c 224c6174 69747564 65223a22 31332e30 3133222c 224c6f6e 67697475 6465223a 2237372e 35373522 2c22436f 6e746163 74506572 736f6e22 3a22536d 69746822 2c225068 6f6e654e 756d6265 72223a22 31313131 31313131 31222c22 4d6f6269 6c654e75 6d626572 223a2239 39393939 39393922 2c224861 7350726f 6d6f223a 2259222c 22436974 79223a22 426e6167 6c6f7265 222c2253 74617465 223a224b 6172616e 6174616b 61222c22 42757369 6e657373 4944223a 2232227d 2c7b2243 6c69656e 744e616d 65223a22 50616d69 6c6c706f 6e31222c 22576562 53697465 223a2261 73646640 6173662e 636f6d22 2c224164 64726573 7331223a 2220426f 756c6576 61726420 4272756e 6f204372 757a222c 22416464 72657373 32223a22 23333230 222c225a 6970223a 2230222c 224c6174 69747564 65223a22 31382e34 34343422 2c224c6f 6e676974 75646522 3a222d36 362e3235 32222c22 436f6e74 61637450 6572736f 6e223a22 47656f72 6765222c 2250686f 6e654e75 6d626572 223a2237 38372d32 35312d30 39343522 2c224d6f 62696c65 4e756d62 6572223a 22373837 2d333430 2d383933 37222c22 48617350 726f6d6f 223a2259 222c2243 69747922 3a22746f 61204261 6a61222c 22537461 7465223a 22507565 72746f20 5269636f 222c2242 7573696e 65737349 44223a22 37227d5d 7d0a>responseData

4

2 回答 2

1

尝试添加-(void)fetchedData:(NSData *)responseData这个代码行:

NSLog(@"responseData: %@", responseData);

并在此处写入 responseData 结果字符串。使用 NSDictionary 和 NSArray 似乎有错误

于 2012-10-19T09:42:48.497 回答
0

从崩溃你可以清楚地看到。

reason: '-[__NSArrayI objectForKey:]'

您正在尝试objectForKey在 NSArray 上触发方法。您将其视为字典,但似乎我是 Array

如果仍然不能解决您的问题。请将 responseData 转换为字符串并检查实际响应,它肯定会对您有所帮助。

于 2012-10-19T11:09:23.140 回答