0
我對iPhone開發很陌生,我使用AFNetworking作爲我的服務庫。AFNetworking - 如何使用POST請求ID
我查詢的API是一個JSON,我需要通過ID獲取數據的POST請求,這是獲取的詳細視圖他新聞圖片和說明和我已經使用GET請求,並在該GEt方法我有NewsID工作正常。要做到這一點,我試着用下面的代碼:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"News_Id": @"2",
@"News_Id": @"3",
@"News_Id": @"5"};
[manager POST:@"http://almithaq.mawaqaademo.com/AlMithaqService/API.svc/getNews_ByID" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
並添加@「text/html的」,在acceptableContentTypes在AFJSONResponseSerializer現在得到以下錯誤:
2014-07-15 12:22:18.030 News[2541:60b] Error: Error Domain=NSCocoaErrorDomain Code=3840
"The operation couldn’t be completed. (Cocoa error 3840.)"
(JSON text did not start with array or object and option to allow fragments not set.)
UserInfo=0x8cf0060 {NSDebugDescription=JSON text did not start with array or object and
option to allow fragments not set., NSUnderlyingError=0x8cefcb0 "Request failed: bad
request (400)"}
您應該使用GET請求獲取數據。 POST請求用於修改服務器上的數據(通常將一些對象添加到集合中)。你需要諮詢服務器API如何做到這一點。如果這是一個RESTful API,您通常使用類似於「http:// example/api/news/123」的url來獲取具有指定ID的特定對象,其中最後一個路徑組件是ID,而「news」組件指定集裝箱。 – CouchDeveloper