AFNetworking将NSMutableDictionary作为POST发送 [英] AFNetworking sending NSMutableDictionary as POST

查看:200
本文介绍了AFNetworking将NSMutableDictionary作为POST发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用AFNetworking的新手。我设法让GET请求在堆栈溢出中得到一个好人的帮助。

Im new to using AFNetworking. I managed to make GET request work with some help from a nice guy here in stack overflow.

现在我陷入了发出POST请求的困境。我尝试这个代码,但我总是在失败的块中。怎么样?

Now im stuck in making a POST request. I try this code but i always end up in failed block. How come?

-(void) postEventInfo: (NSMutableDictionary *) eventInfoObject{

NSURL *url = [NSURL URLWithString:string];

AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:url];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];

[manager POST:string parameters:eventInfoObject success:^(NSURLSessionDataTask *task, id responseObject) {

    NSLog(@"JSON: %@", responseObject);
    //here is place for code executed in success case

} failure:^(NSURLSessionDataTask *task, NSError *error) {


    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error while sending POST"
                                                        message:@"Sorry, try again."
                                                       delegate:nil
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil];
    [alertView show];

    NSLog(@"Error: %@", [error localizedDescription]);
}];
 }

我收到以下错误消息:


错误:请求失败:内部服务器错误(500)

Thnx ..

EDIT1

确定测试之后我注意到连接实际上已经通过并且inte做了我想做的事情,但它仍然显示错误消息意味着仍然会进入失败块。

OK so after testing a little bit more i noticed that the connection actually goes through and inte does what i want to do but it still shows me the error message meaning still going to the fail block.

我认为我的服务器正在发回文本/ html可能与此有关吗?

I THINK my server is sending back text/html could it be something to do with that?

该部分是NSURLConnection工作

the part were NSURLConnection worked

EDIT2

    NSError *error;
NSData *event = [NSJSONSerialization dataWithJSONObject:eventInfoObject
                                                options:NSJSONWritingPrettyPrinted
                                                  error:&error];

if (! event) {
    NSLog(@"Got an error: %@", error);
} else {
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:event];
    [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[event length]] forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setURL:[NSURL URLWithString:string]];

    NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];

    [conn start];
}


推荐答案

如果你的服务器没有设置 Content-Type application / json 的标题(或等效的东西), AFHTTPSessionManager 会失败。理论上,您可以审核AFNetworking acceptableContentTypes 值,但更好的是,您应该修复服务器代码以返回相应的 Content-Type 标题。

If your server is not setting Content-Type header of application/json (or something equivalent), AFHTTPSessionManager will fail. You could theoretically jury-rig the AFNetworking acceptableContentTypes value, but better than that, you should just fix the server code to return the appropriate Content-Type header.

但是如果您的服务器使用500代码进行响应,那么您有一些更基本的问题,如果没有关于服务器的更多信息,我们无法解决这个问题(例如代码,有关实现的一些细节等。)。

But if your server is responding with a 500 code, then you have some more fundamental problem, one that we can't possibly solve without more information about the server (e.g. code, some details about the implementation, etc.).

如果你有一个工作 NSURLConnection 的例子,我' d倾向于运行该代码以及上述代码,并通过 Charles 观察请求。仔细查看请求中的差异,问题可能会突然发生在您身上。

If you have a working NSURLConnection example, I'd be inclined to run both that code, as well as the above code, and observe the requests via Charles. Look carefully at differences in the requests, and the problem will probably jump out at you.

这篇关于AFNetworking将NSMutableDictionary作为POST发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆