0

[已解决] 我正在尝试编辑 AFHTTPClient.m 以使其与 POST 方法一起使用

-(NSMutableURLRequest *)requestWithMethod:(NSString *)method
                                  path:(NSString *)path
                            parameters:(NSDictionary *)parameters

像这样 :

if ([method isEqualToString:@"GET"] ||[method isEqualToString:@"POST"] || [method isEqualToString:@"HEAD"] || [method isEqualToString:@"DELETE"]) 

我对带有参数 userDevice 的 POST 方法有问题,我有来自 AFHTTPClient 的函数子类,如下所示

编辑:

我使用了不同的方法,但结果相同

-(void)loginWithEmail:(NSString *)email password:(NSString *)passwords
{

NSString *baseurl = @"http://localhost.com:9000/";

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:baseurl]];

[httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];

[httpClient setDefaultHeader:@"Accept" value:@"application/json"];

[httpClient setAuthorizationHeaderWithUsername:email password:passwords];

然后这是我的参数:

NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:
                      model, @"deviceModel",
                      systemVersion, @"deviceVersion",
                      [NSNumber numberWithBool:0], @"productionMode",
                      appVersion, @"appVersion",
                      deviceType,@"deviceType", nil];

NSError *error = nil;
NSDictionary* jsonObject = [NSDictionary dictionaryWithObjectsAndKeys:data, @"userDevice", nil];

NSData *jsonLogin =[NSJSONSerialization dataWithJSONObject:jsonObject options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonLoginString = [[NSString alloc] initWithData:jsonLogin encoding:NSUTF8StringEncoding];
NSLog(@"JSON LOGIN OUTPUT :%@", jsonLoginString);

[httpClient setParameterEncoding:AFJSONParameterEncoding];

我要求使用此代码:

NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST"
                                                        path:@"/ios/login"
                                                  parameters:[NSDictionary dictionaryWithObjectsAndKeys:jsonLoginString,@"userDevice", nil]
                                ];    
AFJSONRequestOperation *operation = nil;

operation = [AFJSONRequestOperation
             JSONRequestOperationWithRequest:request
             success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
                 NSLog(@"Response : %@", request);
                 NSLog(@"JSON: %@",JSON);

             }
             failure:^(NSURLRequest *request , NSHTTPURLResponse *response, NSError *error , id JSON ){

                 NSLog(@"error: %@", error);
             }];

[operation start];

这是我想要的示例 json:

"userDevice" : {
    "appVersion" : "1.0",
    "deviceModel" : "iPhone Simulator",
    "productionMode" : false,
    "deviceType" : "iPhone Simulator",
    "deviceVersion" : "6.1"
}

我错过了什么吗?

4

0 回答 0