2014-03-24 68 views
2

我在使用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)} 

任何和所有的幫助表示讚賞。如果我忘記了任何事情或遺漏了解決問題所需的一些東西,請讓我知道,我會盡我所能。

+0

發佈時,您的URL路徑上不應該有斜槓。 – Wain

+0

我剛剛從URL路徑中刪除了尾部斜槓,所以它現在是:https://api.parse.com/1/classes/warm_data,但不幸的是它仍然是相同的錯誤消息。 – CrazyEoin

+0

您的'warm_data'類是否已經存在於數據瀏覽器中? – Wain

回答

2

在嘗試了來自有用評論者的建議以及更多閱讀和嘗試的東西之後,我開始使用AFNetworking進行POST工作!工作代碼如下:

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); 
     } 
]; 

這是與以前相同的代碼,這很奇怪。我一直在尋找通過AFNetworking docs和其他職位談論AFnetworking包括一些StackOverflow的questions here和整個AFHTTPRequestOperationManager的

.securityPolicy.allowInvalidCertificates = YES; 

部分來了。我在經理聲明下面添加了這一行:

manager.securityPolicy.allowInvalidCertificates = YES; 

突然它開始工作!然後我將manager.securityPolicy.allowInvalidCertificates設置爲NO,它仍然有效?!?!然後我刪除了完整的行,並繼續工作,發佈的東西給我解析。很奇怪。我沒有解釋如何解決這個問題,也許它強制解析服務器在刪除證書時允許它,我再也不知道了。我很想知道發生了什麼變化,但現在我要繼續並構建應用程序的其餘部分。如果有人有任何理論,我很樂意聽到他們!

再次感謝所有花時間提出建議的評論者,我很感激。

相關問題