今天用AFNetworking向服務器端發送post請求來獲取數據終於成功了,廢話不多說,直接講過程。
剛開始的時候總是報404錯誤,
failure -- error = Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: not found (404)" UserInfo=0x7fb471f18110 {NSLocalizedDescription=Request failed: not found (404), NSErrorFailingURLKey=http://114.85.246.110:11111/IAppService/InsertPurchaseRemainPa, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x7fb471f7d300> { URL: http://114.85.246.110:11111/IAppService/InsertPurchaseRemainPa } { status code: 404, headers {
"Content-Length" = 1565;
"Content-Type" = "text/html; charset=UTF-8";
Date = "Wed, 22 Apr 2015 06:53:07 GMT";
Server = "Microsoft-HTTPAPI/2.0";
} }, NSUnderlyingError=0x7fb471f06ab0 "Request failed: unacceptable content-type: text/html"}
網頁上顯示“未找到終結點”,然后我在網上百度了好多種解決方法,比如
1.看看是用http還是json
manager.responseSerializer = [AFJSONResponseSerializer serializer];
2.text/html 那個去掉看看報什么錯,有時候這句不一定需要。
3.params有沒有問題,有沒有映射錯誤。
具體看接口的要求,用get還是post,什么類型的數據,傳什么參。我就是一個個試驗出來的。
以上是網上某位大神說的解決方法,可以一試,基本上就差不多,可我的問題就是特別,就tmd是服務器那邊的問題,怎么試都不管用,所以有問題的時候不要自己一個人在那瞎試,要讓服務器那邊最好也試試,最好是他那能測試成功了你在改你自己的,一定要確認他給你的什么都是對的要不然坑死你。
上面的404錯誤就是訪問不了服務器,服務器弄好了,接口地址也確認了呢就能訪問了不會出現404錯誤了。
然后呢又出現了新的錯誤,先貼代碼:
NSString *urlstr2 = @"http://114.85.246.110:11111/IAppService/InsertPurchaseRemainPay";
//[urlstr2 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dic2 = @{@"ShopID":@"802BB63B-7DBE-496D-BCB5-3F3F00848FFC",@"Balance":@10,@"Rebate":@0,@"BankAccountID":@"08F2A9BB-F318-4BE0-A4A5-33C0125FF137",@"BankTradeNo":@"",@"OrderPayID":@"00000000-0000-0000-0000-000000000000",@"PaidSum":@15,@"PurchaseID":@"5A720BCF-C301-4A1D-AD37-24F04C8A6F5D",@"State":@0};
NSLog(@"%@",dic2);
//print out the data contents
NSString *str2 = [dic3 JSONString];
NSLog(@"strrrr=====%@",str2);
AFHTTPRequestOperationManager *manager1 = [AFHTTPRequestOperationManager manager];
manager1.requestSerializer = [AFJSONRequestSerializer serializer];
manager1.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager1 POST:urlstr2 parameters:str2 success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"success -- %@",operation.responseString);
NSString *str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"success -- str = %@ ",str);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"failure -- %@",operation.responseString);
NSLog(@"failure -- error = %@",error);
}];
上面dic是服務器給的參數模型,也就是個字典,他說要轉換成json傳給他,我傻,不知道字典就tmd是json數據還字費勁把字典給轉成了json字符串傳給他,就這樣參數老不對,他那邊要的就是一個字典模型,所以直接傳給他一個字典就行了,但是上面得加上 manager1.requestSerializer = [AFJSONRequestSerializer serializer];這一句話進行配置,發送的時json請求,大意就是這樣吧,具體的自行百度。
然后呢,接口地址確認對了,參數也傳對了,就差返回類型了,AFHTTPResponseSerializer這個東西是要求你對返回的數據自己進行解析,想怎么解析就怎么解析,AFJSONResponseSerializer,而這個呢是af這個庫替你解析返回的json數據,所以block里面再寫 NSString *str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];這句就會崩,因為解析過后的responseObject就是一個數組,你直接返回responseObject就行了。
最后代碼精簡為:
NSString *urlstr2 = @"http://114.85.246.110:11111/IAppService/InsertPurchaseRemainPay";
NSDictionary *dic2 = @{@"ShopID":@"802BB63B-7DBE-496D-BCB5-3F3F00848FFC",@"Balance":@10,@"Rebate":@0,@"BankAccountID":@"08F2A9BB-F318-4BE0-A4A5-33C0125FF137",@"BankTradeNo":@"",@"OrderPayID":@"00000000-0000-0000-0000-000000000000",@"PaidSum":@15,@"PurchaseID":@"5A720BCF-C301-4A1D-AD37-24F04C8A6F5D",@"State":@0};
NSLog(@"%@",dic2);
AFHTTPRequestOperationManager *manager1 = [AFHTTPRequestOperationManager manager];
manager1.requestSerializer = [AFJSONRequestSerializer serializer];
manager1.responseSerializer = [AFJSONResponseSerializer serializer];
[manager1 POST:urlstr2 parameters:dic2 success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"success -- %@",responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"failure -- error = %@",error);
}];
