[英]AFNetworking: Get JSON Value From Http Response
I am primarily an android developer and really new to IOS. 我主要是一名Android开发人员,并且是IOS的新手。 I am using AFNetworking 1.0 since I am working with an existing source and I am sending a standard http Post request and returning the data like so: 我正在使用AFNetworking 1.0,因为我正在使用现有资源,并且正在发送标准的http Post请求并返回数据,如下所示:
//Sending the post request and getting the result
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://testsite.org/mobile_app"]];
[httpClient setParameterEncoding:AFFormURLParameterEncoding];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST"
path:@"http://testsite.org/mobile_app/studentregister.php"
parameters:@{@"username":username, @"displayname":displayname, @"password":password, @"passwordc":passwordc, @"email":email, @"teacherCode":teacherCode}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
// Print the response body in text
NSLog(@"Response: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
[operation start];
the response should contain the JSON data which looks like this according to burp suit: 根据burp西装,响应中应包含如下所示的JSON数据:
{"userCharLimit":"Your username must be between 5 and 25 characters in length"}
{"displaynameCharLim":"Your displayname must be between 5 and 25 characters in length"}
{"passLimit":"Your password must be between 8 and 50 characters in length"}
{"emailInvalid":"Not a valid email address"}
{"teacherCodeLength":"Your teacher code is not 5 to 12 characters"}.
How can I get those JSON values? 如何获取这些JSON值? I have seen a lot of AFNetworking 2.0 JSON examples and sending JSON Post request but I can seem to find much for version 1.0 let alone getting the values form a standard post request. 我已经看过很多AFNetworking 2.0 JSON示例和发送JSON Post请求,但是我似乎可以在1.0版中找到很多东西,更不用说从标准post请求中获取值了。 Any ideas or resources? 有什么想法或资源吗?
All you should need is to change your code to register a AFJSONRequestOperation instead of AFHTTPRequestOperation. 您需要做的就是更改代码以注册AFJSONRequestOperation而不是AFHTTPRequestOperation。
[httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
Then you can use this method on AFHTTPClent to create the operation which will actually be of type AFJSONRequestOperation. 然后,您可以在AFHTTPClent上使用此方法来创建实际上为AFJSONRequestOperation类型的操作。
AFHTTPRequestOperation *operation = [httpClient HTTPRequestOperationWithRequest:request success:nil failure:nil];
[httpClient enqueueOperation:operation];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.