在AFNetworking中使用URL参数和JSON正文进行POST

编程入门 行业动态 更新时间:2024-10-27 16:30:24
本文介绍了在AFNetworking中使用URL参数和JSON正文进行POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想进行一个同时具有URL参数和JSON正文的POST调用:

I'd like to make a POST call that has both URL parameters and a JSON body:

URL example/register?apikey=mykey JSON { "field" : "value"}

如何通过AFNNetworking同时使用两个不同的串行器?这是我缺少URL参数的代码:

How can I use two different serializers at the same time with AFNNetworking? Here's my code with the URL parameters missing:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.responseSerializer = [AFJSONResponseSerializer serializer]; [manager POST:@"example/register" parameters:json success:^(AFHTTPRequestOperation *operation, id responseObject) {

推荐答案

我创建了post方法

/** * Services gateway * Method get response from server * @parameter -> object: request josn object ,apiName: api endpoint * @returm -> void * @compilationHandler -> success: status of api, response: respose from server, error: error handling */ + (void)getDataWithObject:(NSDictionary *)object onAPI:(NSString *)apiName withController:(UIViewController*)controller :(void(^)(BOOL success,id response,NSError *error))compilationHandler { controller = controller; [UIApplication sharedApplication]workActivityIndicatorVisible = YES; AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; // set request type to json manager.requestSerializer = [AFJSONRequestSerializer serializer]; manager.responseSerializer = [AFHTTPResponseSerializer serializer]; // post request to server [manager POST:apiName parameters:object success:^(AFHTTPRequestOperation *operation, id responseObject) { // NSError *error; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:responseObject options:0 error:&error]; //NSString *JSONString = [[NSString alloc] initWithBytes:[jsonData bytes] length:[jsonData length] encoding:NSUTF8StringEncoding]; //// // check the status of API NSDictionary *dict = responseObject; NSString *statusOfApi = [[NSString alloc]initWithFormat:@"%@" ,[dict objectForKey:@"OK"]]; // IF Status is OK -> 1 so complete the handler if ([statusOfApi isEqualToString:@"1"] ) { [UIApplication sharedApplication]workActivityIndicatorVisible = NO; compilationHandler(TRUE,responseObject,nil); } else { [UIApplication sharedApplication]workActivityIndicatorVisible = NO; NSArray *errorMessages = [responseObject objectForKey:@"messages"]; NSString *message = [errorMessages objectAtIndex:0]; [Utilities showAlertViewWithTitle:apiName message:message]; compilationHandler(FALSE,responseObject,nil); } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSString *message = [NSString stringWithFormat:@"%@",[error localizedDescription]]; NSLog(@"Message is %@", message); NSString *errorMessage = [NSString stringWithFormat:@"%@",[error localizedDescription]]; if (!([message rangeOfString:@"The request timed out."].location == NSNotFound)) { [Utilities showAlertViewWithTitle:apiName message:errorMessage]; } compilationHandler(FALSE,errorMessage,nil); }]; // For internet reachibility check if changes its state [self checkInternetReachibility:manager]; }

**例如,当我们调用服务**

**for Example when we call the Service **

// calling service gateway API NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys: "field",@"value", nil]; [self getDataWithObject:dict onAPI:KGet_Preferences withController:(UIViewController*)controller :^(BOOL success, id response, NSError *error) { if( success ) { NSMutableDictionary *data = [[response valueForKey:@"data"] valueForKey:@"preferences"]; compilationHandler(success,data,error); } else { compilationHandler(success,nil,error); } }];

更多推荐

在AFNetworking中使用URL参数和JSON正文进行POST

本文发布于:2023-10-29 09:32:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539345.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   正文   URL   AFNetworking   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!