[解決済み] 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"])
params 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"
}
私は何かを逃していますか?