gpt4 book ai didi

json - Restkit 0.20 上的简单 JSON 登录帖子

转载 作者:行者123 更新时间:2023-12-01 04:49:09 26 4
gpt4 key购买 nike

我按照 this old answer. 的步骤进行操作主要问题是 RKObjectLoaderDelegate 已被删除,我找不到任何适合我的替代方案。我正在尝试使用邮件/密码连接到服务器,将发布请求发送到 json。另外,在我测试它是否正确后,我将使用 json 从数据库中获取数据。有人可以帮我吗?我正在使用 RESTkit

    //Autentificacion
NSURL* url = [[NSURL alloc]initWithString:@"http://myurl.com/tokens.json"];
RKObjectManager* objectManager = [RKObjectManager managerWithBaseURL:url];
objectManager = [RKObjectManager managerWithBaseURL:url];

就像我之前说的,我试图做一个 JSON 登录帖子,但不同的例子对我不起作用,我在 restkit 0.20 上寻找一些简单的方法来做。我知道如何进行 GET 但不会进行 POST。

最佳答案

首先,我创建了一个名为 Login Request 的类。

LoginRequest.h

#import <Foundation/Foundation.h>
#import "RKObjectMapping.h"

@interface LoginRequest : NSObject
@property (nonatomic, strong) NSString* email;
@property (nonatomic, strong) NSString* password;

+(RKObjectMapping*)defineLoginRequestMapping;
@end

LoginRequest.m

#import "LoginRequest.h"
#import "RKObjectManager.h"

@implementation LoginRequest
@synthesize email;
@synthesize password;

+(RKObjectMapping*)defineLoginRequestMapping {

RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[LoginRequest class]];

[mapping addAttributeMappingsFromDictionary:@{
@"email": @"email",
@"password": @"password",
}];


return mapping;

}

然后,我创建了一个 LoginManager 类来进行连接。

LoginManager.h

#import <Foundation/Foundation.h>
#import "RKObjectManager.h"
#import "LoginResponse.h"
#import "LoginRequest.h"

@interface LoginManager : NSObject
@property (nonatomic, strong) LoginRequest *dataObject;
@property (nonatomic, strong) RKObjectManager *objectManager;
@property (nonatomic, strong) AFHTTPClient * client;
-(void)LoginWithUserName:(NSString *)username password:(NSString*)password;

@end

LoginManager.m

#import "LoginManager.h"
#import "RKMIMETypeSerialization.h"
#import "RKLog.h"
#import "LoginRequest.h"


@implementation LoginManager

-(void)LoginWithUserName:(NSString *)email password:(NSString*)password {

LoginRequest *dataObject = [[LoginRequest alloc] init];
[dataObject setEmail:email];
[dataObject setPassword:password];



NSURL *baseURL = [NSURL URLWithString:@"http://www.myurl.com/tokens.json"];

AFHTTPClient * client = [AFHTTPClient clientWithBaseURL:baseURL];
[client setDefaultHeader:@"Accept" value:RKMIMETypeJSON];

RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];


RKObjectMapping *requestMapping = [[LoginRequest defineLoginRequestMapping] inverseMapping];

[objectManager addRequestDescriptor: [RKRequestDescriptor
requestDescriptorWithMapping:requestMapping objectClass:[LoginRequest class] rootKeyPath:nil
]];
// what to print
RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);
RKLogConfigureByName("Restkit/Network", RKLogLevelDebug);

RKObjectMapping *responseMapping = [LoginResponse defineLoginResponseMapping];

[objectManager addResponseDescriptor:[RKResponseDescriptor
responseDescriptorWithMapping:responseMapping method:RKRequestMethodAny pathPattern:@"" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)

]];


[objectManager setRequestSerializationMIMEType: RKMIMETypeJSON];

[objectManager postObject:dataObject path:@""
parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"It Worked: %@", [mappingResult array]);

} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"It Failed: %@", error);

}];
}

最后,我在 MasterViewController.m 上调用 viewDidLoad 上的方法

[[LoginManager alloc] LoginWithUserName:@"mymail@test.com" password:@"mypassword"];

关于json - Restkit 0.20 上的简单 JSON 登录帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19632529/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com