I'm having a problem using AFNetworking with Parse.com. I'm trying to POST data to the db with no luck. I'm not too familiar with networking stuff and so I'm using this as a learning exercise.
我在使用Parse.com网站时遇到了一个问题。我想把数据发到db上,但运气不好。我不太熟悉社交方面的东西,所以我把它作为一个学习练习。
I've gotten the GET command to work so I know that the db is set up correctly and working but POSTing is another matter. I'm using AFNetworking 2.2.0 to POST a simple test string to the parse backend.
我已经让GET命令开始工作,所以我知道db设置正确并正常工作,但是发布是另一回事。我正在使用AFNetworking 2.2.0向解析后端发布一个简单的测试字符串。
Here is the code I have for the GET command which works:
下面是我的GET命令代码,它可以工作:
-(void)getParseData{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setValue:kSDFParseAPIApplicationId forHTTPHeaderField:@"X-Parse-Application-Id"];
[manager.requestSerializer setValue:kSDFParseAPIKey forHTTPHeaderField:@"X-Parse-REST-API-Key"];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[manager GET:@"https://api.parse.com/1/classes/some_data/" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
}
and here is the code I have so far for the POSt command, which is't working:
这是我到目前为止对POSt命令的代码,它不起作用:
-(void)postParseData{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setValue:kSDFParseAPIApplicationId forHTTPHeaderField:@"X-Parse-Application-Id"];
[manager.requestSerializer setValue:kSDFParseAPIKey forHTTPHeaderField:@"X-Parse-REST-API-Key"];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
manager.responseSerializer = [[AFJSONResponseSerializer serializer]init];
NSDictionary *params = @{@"test_string" : @"NameTest"};
[manager POST:@"https://api.parse.com/1/classes/warm_data/" parameters: params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"POST data JSON returned: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
I know there is a custom parse.com API for iOS for this exact purpose but I want to use AFNetworking to learn more and improve. I've tried several different things I'v found on the net and specifically from stack overflow but noting yielded a result. Here is the error message returned from the AFHTTPRequestOperation failure block:
我知道有一个为iOS定制的parse.com API,但是我想用AFNetworking来学习和改进。我尝试了一些我在网上找到的不同的东西,特别是从stack overflow上找到的,但是没有注意到结果。以下是AFHTTPRequestOperation失败块返回的错误消息:
2014-03-24 11:42:07.894 testApp[3222:60b] Error: Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: bad request (400)" UserInfo=0xfe0f400 {NSErrorFailingURLKey=https://api.parse.com/1/classes/some_data/, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x14780070> { URL: api.parse.com/1/classes/some_data/ } { status code: 400, headers {
"Access-Control-Allow-Origin" = "*";
"Access-Control-Request-Method" = "*";
"Cache-Control" = "no-cache";
Connection = "keep-alive";
"Content-Length" = 130;
"Content-Type" = "application/json; charset=utf-8";
Date = "Mon, 24 Mar 2014 11:42:07 GMT";
Server = "nginx/1.4.2";
"Set-Cookie" = "_parse_session=BAh7BkkiD3Nlc3Npb25faWQGOgZFRiIlYWQyNmFhMTA3ZTJkZjljYjA3MjZkZTA1MGQzNWIzNGE%3D--f7812e190b5812d8bba91aea2b12d51412bf84ce; domain=.parse.com; path=/; expires=Wed, 23-Apr-2014 11:42:07 GMT; secure; HttpOnly";
Status = "400 Bad Request";
"X-Runtime" = "0.049574";
"X-UA-Compatible" = "IE=Edge,chrome=1";
} }, NSLocalizedDescription=Request failed: bad request (400)}
Any and all help is appreciated. If I've forgotten any thing or omitted something needed to fix the problem just let me know and I'll add what I can.
非常感谢您的帮助。如果我忘记了任何事情或遗漏了一些需要解决问题的东西,请让我知道,我会尽我所能。
1 个解决方案
#1
2
After trying suggestions from helpful commenters and lots more reading and just trying stuff I got POST working with AFNetworking! The working code is as follows:
在尝试了来自有用的评论者的建议和更多的阅读和尝试之后,我得到了与网络合作的帖子!工作代码如下:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:kSDFParseAPIApplicationId forHTTPHeaderField:@"X-Parse-Application-Id"];
[manager.requestSerializer setValue:kSDFParseAPIKey forHTTPHeaderField:@"X-Parse-REST-API-Key"];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSDictionary *dic2 = @{@"test_number": @1234};
[manager POST:@"https://api.parse.com/1/classes/warm_data" parameters: dic2
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"POST data JSON returned: %@", responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}
];
This is the same code as before which is weird. I was looking through the AFNetworking docs and other posts talking about AFnetworking including some * questions here and came across the
这和之前的代码一样奇怪。我浏览了AFNetworking文档和其他讨论AFNetworking的帖子,其中包括这里的一些*问题
.securityPolicy.allowInvalidCertificates = YES;
part of the AFHTTPRequestOperationManager. I added this line to below the manager declaration:
AFHTTPRequestOperationManager的一部分。我在经理声明下面添加了这一行:
manager.securityPolicy.allowInvalidCertificates = YES;
And suddenly it started working! I then set the manager.securityPolicy.allowInvalidCertificates to NO and it still worked?!?! then I removed the line complete and it keeps working, posting stuff to Parse for me. Very weird. I have no explanation how this fixed it, maybe it forced the parse servers to allow it when the cert was removed, again I have no idea. I'd love to know what changed but for now I'm going to continue on and build the rest of the app. If anyone has any theories I'd love to hear them!
突然它开始工作了!然后设置manager.securityPolicy。不允许证书失效,但它仍然有效?!?!然后我删除了完整的行,它继续工作,发布东西来为我解析。非常奇怪。我没有解释它是如何修复的,也许它迫使解析服务器在cert被移除时允许它,我也不知道。我很想知道是什么改变了,但现在我要继续开发这个应用程序的其余部分。如果有人有什么想法,我很想听听!
Thanks again to all the commenters who took the time to suggest stuff, I appreciate it.
再次感谢所有花时间提出建议的人,我很感激。
#1
2
After trying suggestions from helpful commenters and lots more reading and just trying stuff I got POST working with AFNetworking! The working code is as follows:
在尝试了来自有用的评论者的建议和更多的阅读和尝试之后,我得到了与网络合作的帖子!工作代码如下:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:kSDFParseAPIApplicationId forHTTPHeaderField:@"X-Parse-Application-Id"];
[manager.requestSerializer setValue:kSDFParseAPIKey forHTTPHeaderField:@"X-Parse-REST-API-Key"];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSDictionary *dic2 = @{@"test_number": @1234};
[manager POST:@"https://api.parse.com/1/classes/warm_data" parameters: dic2
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"POST data JSON returned: %@", responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}
];
This is the same code as before which is weird. I was looking through the AFNetworking docs and other posts talking about AFnetworking including some * questions here and came across the
这和之前的代码一样奇怪。我浏览了AFNetworking文档和其他讨论AFNetworking的帖子,其中包括这里的一些*问题
.securityPolicy.allowInvalidCertificates = YES;
part of the AFHTTPRequestOperationManager. I added this line to below the manager declaration:
AFHTTPRequestOperationManager的一部分。我在经理声明下面添加了这一行:
manager.securityPolicy.allowInvalidCertificates = YES;
And suddenly it started working! I then set the manager.securityPolicy.allowInvalidCertificates to NO and it still worked?!?! then I removed the line complete and it keeps working, posting stuff to Parse for me. Very weird. I have no explanation how this fixed it, maybe it forced the parse servers to allow it when the cert was removed, again I have no idea. I'd love to know what changed but for now I'm going to continue on and build the rest of the app. If anyone has any theories I'd love to hear them!
突然它开始工作了!然后设置manager.securityPolicy。不允许证书失效,但它仍然有效?!?!然后我删除了完整的行,它继续工作,发布东西来为我解析。非常奇怪。我没有解释它是如何修复的,也许它迫使解析服务器在cert被移除时允许它,我也不知道。我很想知道是什么改变了,但现在我要继续开发这个应用程序的其余部分。如果有人有什么想法,我很想听听!
Thanks again to all the commenters who took the time to suggest stuff, I appreciate it.
再次感谢所有花时间提出建议的人,我很感激。