使用AFNetworking的iOS POST请求

时间:2013-02-25 19:32:31

标签: ios objective-c web-services afnetworking

我正在使用AFNetworking向网络服务发出POST请求

这是我需要发送的JSON -

{
   “LinkedTo” : {
                            “IndividualStakeholder” : “”,
                            “GroupedStakeholder” : “”,
                            “LandParcelStakeholder” : “”,
                            “Communications” : “”,
                            “Team” : “”,
                            “Issue” : “”,
                            “Event” : “”
                        }
   “userId” : 170,
   “projectId”: 12
}

这是我正在使用的代码 -

NSString *jsonData = [self JSONString:jsonData1];

此时NSString *jsonData中的值为

  

{\ “LinkedTo \”:{\ “IndividualStakeholder \”:\ “\” \ “GroupedStakeholder \”:\ “\” \ “LandParcelStakeholder \”:\ “\” \ “通信\”: \ “\”,\ “小组\”:\ “\”,\ “问题\”:\ “\”,\ “事件\”:\ “\”} \ “用户id \”:170,\“专案编号\“:4}

NSURL *url = [[NSURL alloc]initWithString:@"https://somedomain.com/"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc]initWithBaseURL:url];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                             jsonData, @"jsonText" , nil];
NSURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"https://somedomain.com/api/stakeholders" parameters:params];

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request 
    success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON){
        NSLog(@"Inside the success block %@",JSON);
    }
    failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON){
        NSLog(@"json text is: %@", JSON);
        NSLog(@"Request failed with error: %@, %@", error, error.userInfo);
    }];
[operation start];



-(NSString*)JSONString :(NSString*)aString{
    NSMutableString *s = [NSMutableString stringWithString:aString];
    [s replaceOccurrencesOfString:@"\"" withString:@"\\\"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])];
    [s replaceOccurrencesOfString:@"/" withString:@"\\/" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])];
    [s replaceOccurrencesOfString:@"\n" withString:@"\\n" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])];
    [s replaceOccurrencesOfString:@"\b" withString:@"\\b" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])];
    [s replaceOccurrencesOfString:@"\f" withString:@"\\f" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])];
    [s replaceOccurrencesOfString:@"\r" withString:@"\\r" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])];
    [s replaceOccurrencesOfString:@"\t" withString:@"\\t" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])];
    return [NSString stringWithString:s];
}

服务器上的错误 - 无法识别以“/利益相关者”意外结束的网址的请求格式。在反序列化发布的JSON时,asp.net抛出了哪个错误

我在客户端收到的输出是

请求失败并显示错误:错误Domain = com.alamofire.networking.error Code = -1011“预期状态代码[索引数:100(在1个范围内),索引:(200-299)],得到500” UserInfo = 0x8e95e60 {NSErrorFailingURLKey = https://somedomain.com/api/stakeholders,NSLocalizedDescription =预期状态代码[索引数:100(在1个范围内),索引:(200-299)],得到500},{     NSErrorFailingURLKey =“https://somedomain.com/api/stakeholders”;     NSLocalizedDescription =“预期状态代码[索引数:100(在1个范围内),索引:(200-299)],得到500”; } 2013-02-25 11:03:44.669 NetworkPlugin [60069:c07]

2 个答案:

答案 0 :(得分:3)

使用AFHTTPClient查看setParameterEncoding的{​​{1}}方法,以便帖子实际上具有有效的JSON HTTP正文。声明并评论here

答案 1 :(得分:1)

我看到的是您发送","而不是"15", "18"等值。尝试使用正确的JSONKit函数修改您的json数据。