2014-04-01 61 views
1

我想用我的iOS應用程序獲取此JSON數據:https://sleepy-journey-2871.herokuapp.com/users.json .... RestKit嘗試從url中獲取這些用戶,但它返回0個對象並且顯示「無可映射在搜索到的關鍵路徑上找到表示,沒有響應描述符匹配加載的響應。「RestKit - 從Rails應用程序中獲取JSON數組

請幫我弄清楚我失蹤或做錯了什麼!我一直在爲此奮戰數星期。

我的Xcode 5.0.2,我成功安裝RestKit用的CocoaPods和Podfile,看起來像這樣:

platform :ios, '6.0' 
pod 'RestKit', '~> 0.22.0' 
pod 'RestKit/Testing' 
pod 'RestKit/Search' 

我AppDelegate.m文件如下(在didFinishLaunchingWithOptions方法):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    // Override point for customization after application launch. 

    //Set base url 
    NSString *baseUrl = @"https://sleepy-journey-2871.herokuapp.com"; 

    //initialize the the http client with baseUrl 
    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:baseUrl]]; 

    //initialize the RKObjectManager with our http client 
    RKObjectManager *manager = [[RKObjectManager alloc] initWithHTTPClient:httpClient]; 

    //add text/plain as a JSON content type to properly parse errors 
    [RKMIMETypeSerialization registerClass:[RKNSJSONSerialization class] forMIMEType:@"text/plain"]; 

    //register JSONRequestOperation to parse JSON in requests 
    [manager.HTTPClient registerHTTPOperationClass:[AFJSONRequestOperation class]]; 

    //state that we are accepting JSON content type 
    [manager setAcceptHeaderWithMIMEType:RKMIMETypeJSON]; 

    //configure so that we want the outgoing objects to be serialized into JSON 
    manager.requestSerializationMIMEType = RKMIMETypeJSON; 

    //set the shared instance of the object manager, so that we can easily re-use it later 
    [RKObjectManager setSharedManager:manager];  

    return YES; 
} 

我在視圖控制器的viewDidLoad方法,首先負載時所述應用啓動此代碼:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    RKObjectManager *manager = [RKObjectManager sharedManager]; 
    [manager getObjectsAtPath:@"/users" 
        parameters:nil 
         success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) 
    { 
     NSLog(@"Loaded databases: %@", [mappingResult array]); 
    } 
         failure:^(RKObjectRequestOperation *operation, NSError *error) 
    { 
     NSLog(@"Error: %@", [error localizedDescription]); 
    }]; 
// Do any additional setup after loading the view, typically from a nib. 
} 

回答

0

您必須:

  1. 創建一個用戶類。
  2. 爲User類創建一個映射。
  3. 創建響應描述符。

僞代碼可能看起來是這樣的:

@interface User : NSObject 
@property (nonatomic, copy) NSNumber *userID; 
@property (nonatomic, copy) NSString *name; 
... 
@end 

RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[User class]]; 
[mapping addAttributeMappingsFromDictionary:@{ 
               @"name": @"name", 
               @"id":  @"userID" 
               ... 
               }]; 

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping 
                         method:RKRequestMethodAny 
                        pathPattern:nil 
                         keyPath:nil 
                        statusCodes:nil]; 
... 
RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:request 
                    responseDescriptors:@[responseDescriptor]]; 

[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *result) { 
    NSLog(@"The public timeline Tweets: %@", [result array]); 
} failure:nil]; 

[operation start]; 

看看例子here

+0

那麼'RKObjectRequestOperation *操作...'部分及其下面的部分仍然在該用戶類中? 「運行開始」代碼何時運行? – EAB

相關問題