我在使用AFNetworking和Parse.com時遇到問題。我試圖發送數據到數據庫沒有運氣。我對網絡的東西不太熟悉,所以我將其用作學習練習。如何使用AFNetworking將數據發佈到Parse.com
我已經得到了GET命令的工作,所以我知道數據庫設置正確,工作,但POSTing是另一回事。我使用AFNetworking 2.2.0將簡單的測試字符串發送到解析後端。
這裏是我對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);
}];
}
,這裏是我的代碼至今該職位的命令,這豈不等於工作:
-(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);
}];
}
我知道有一個自定義的parse.com API用於iOS的確切目的,但我想使用AFNetworking來了解更多和改進。我嘗試了幾種不同的東西,我在網上發現了它,特別是堆棧溢出,但是注意到了結果。這裏是從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)}
任何和所有的幫助表示讚賞。如果我忘記了任何事情或遺漏了解決問題所需的一些東西,請讓我知道,我會盡我所能。
發佈時,您的URL路徑上不應該有斜槓。 – Wain
我剛剛從URL路徑中刪除了尾部斜槓,所以它現在是:https://api.parse.com/1/classes/warm_data,但不幸的是它仍然是相同的錯誤消息。 – CrazyEoin
您的'warm_data'類是否已經存在於數據瀏覽器中? – Wain