AFNetworking无法识别JSON参数POST
[已解决] 我正在尝试编辑AFHTTPClient.m,以使其与中的POST方法一起工作AFNetworking无法识别JSON参数POST,json,ios6,afnetworking,Json,Ios6,Afnetworking,[已解决] 我正在尝试编辑AFHTTPClient.m,以使其与中的POST方法一起工作 -(NSMutableURLRequest *)requestWithMethod:(NSString *)method path:(NSString *)path parameters:(NSDictionary *)parameters 像这样: if ([method isEq
-(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"
}
我遗漏了什么吗?你得到了什么错误?@LithuT.V错误:我的服务器上的userDevice错误,当我记录jsonObject时,结果的格式错误,userDevice={appVersion=“1.0”;deviceModel=“iPhone Simulator”;deviceType=“iPhone Simulator”;deviceVersion=“6.1”;productionMode=0;};他们使用“=”not“:”作为我上面的json示例,有什么想法吗?