1

私はRestKitを使い始めたばかりで、Rk 0.20が公開され、ドキュメントとデモが一歩遅れているのと同じように到着しました。Web上のほとんどのものはRK0.10用であり、0.20バージョンには大きな変更があります。

新しいバージョンがすぐに稼働するときに、以前のバージョンにフォールバックしたくありません。

単純なデータグラムを返すURL"test.myserver.com"にJSONリソースがあります-{"id_user": "4401"、 "datalocation": "4401"、 "country": "Great-Britain"、 "data ":" testdata "、" login ":" Fred Bloggs "、" password ":" 579c0cb0ed2dc25db121283f7a98cc71 "、" accessLevel ":" 2 "、" timestamp ":" 1012 "、" datahash ":" 2749da29f20ce7a85092323f193adee8 "}

マッピングなどを並べ替えたと確信していますが、サービスには認証が必要なため、リクエストでユーザー名とパスワードをサーバーに渡す必要があります。

私はこれまでにこれを持っています

NSURL *url = [NSURL URLWithString:@"http://test.myserver.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];

[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
RKLogInfo(@"Load collection of Articles: %@", mappingResult.array);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
RKLogError(@"Operation failed with error: %@", error);
}];

         [objectRequestOperation start];

これはサーバーに接続しているように見えますが、必然的に次のエラーをログに記録します

restkit.network:RKObjectRequestOperation.m:296オブジェクト要求が失敗しました:基になるHTTP要求操作がエラーで失敗しました:エラードメイン=org.restkit.RestKit.ErrorDomainコード=-1011"(200-299)で予期されるステータスコード、401を取得しました" UserInfo = 0x7884030 {NSLocalizedRecoverySuggestion = {"error":{"code":401、 "message": "Unauthorized:Authentication required"}}、AFNetworkingOperationFailingURLRequestErrorKey = http://elancovision.umfundi.com>、NSErrorFailingURLKey = http:// elancovision .umfundi.com、NSLocalizedDescription =(200-299)の予想されるステータスコード、401を取得、AFNetworkingOperationFailingURLResponseErrorKey =}

もちろん問題は、リクエストにユーザー名とパスワードをどのように追加するかです。

初心者の質問でごめんなさい!

4

2 に答える 2

6

基本 HTTP 認証では、各要求の HTTP 要求認証ヘッダー フィールドにユーザー名とパスワードを挿入する必要があります。

まず、RKObjectManager を使用して、リクエストとマッピングの構成を一元化することをお勧めします。http://restkit.org/api/latest/Classes/RKObjectManager.html RKObjectManager は、(AFNetworking ライブラリを介して) ネットワーク パラメータを格納し、ユーザー名/パスワード、パス、オブジェクト マッピングに基づいて適切な http クエリを作成できます。

あなたの例を適応させると、次のようになります:

NSURL* url = [[NSURL alloc]initWithString:@"http://test.myserver.com"];
RKObjectManager* objectManager = [RKObjectManager managerWithBaseURL:url];
[objectManager.HTTPClient setAuthorizationHeaderWithUsername:@"username" password:@"password"];

//NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLRequest *request = [objectManager requestWithObject:nil method:RKRequestMethodGET path:@"/yourAPI/yourmethod" parameters:nil];

RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];

[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
        RKLogInfo(@"Load collection of Articles: %@", mappingResult.array);
    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
        RKLogError(@"Operation failed with error: %@", error);
    }];

[objectRequestOperation start];

認証が機能する場合は、RESTKit wiki を参照すると、正しいマッピングを作成するための次のヒントが得られます: https://github.com/RestKit/RestKit/wiki/Object-mapping

于 2013-02-07T19:24:51.993 に答える
1

ここでの私の解決策:

// Build a RestKit manager object to look after the restful stuff
RKObjectManager *manager =  [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://test.myserver.com"]];;

// Hash the GUI input password string and pass the username in plain text
NSString *md5PW = [umfundiCommon md5:passwordField.text];           
[manager.HTTPClient setAuthorizationHeaderWithUsername:userField.text password:md5PW];

RKObjectMapping *WebResponse = [RKObjectMapping mappingForClass:[WSObject class]];

        [WebResponse addAttributeMappingsFromDictionary:@{@"id_user":@"id_user", @"datalocation": @"datalocation", @"country":@"country", @"data": @"data", @"login": @"login", @"password": @"password", @"accessLevel": @"accessLevel", @"timestamp": @"timestamp", @"datahash": @"datahash"}];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:WebResponse pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
// Add the above response descriptor to the manager
[manager addResponseDescriptor:responseDescriptor];

// the getObject makes the call using the stuff assembled into the manager Object and drops into either the success or the failure routines.
[manager getObject:nil path:@"" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *result)
{
NSLog (@"Server WS call success:");

NSArray *theresults = [result array];              
for (WSObject *item in theresults) {
    NSLog(@"datahash=%@",item.datahash);
    NSLog(@"user_id=%@",item.id_user);
    }
}  failure:^(RKObjectRequestOperation * operation, NSError * error)
    {
    NSLog (@"Server WS call failure: operation: %@ \n\nerror: %@", operation, error);
    }];

........
于 2013-02-08T09:54:56.407 に答える