【发布时间】:2016-11-02 13:29:04
【问题描述】:
我正在使用 AFNetworking 在 iOS 中使用 post 方法从服务器获取数据,但我得到了
Error: Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x1607abf0> { URL: https://fcm.googleapis.com/fcm/send } { status code: 400, headers {
"Content-Length" = 54;
"Content-Type" = "text/html; charset=UTF-8";
Date = "Wed, 02 Nov 2016 13:24:20 GMT";
} }, NSErrorFailingURLKey=https://fcm.googleapis.com/fcm/send, NSLocalizedDescription=Request failed: bad request (400), com.alamofire.serialization.response.error.data=<3c68746d 6c3e3c74 69746c65 3e457272 6f722034 30302028 42616420 52657175 65737429 2121313c 2f746974 6c653e3c 2f68746d 6c3e>, NSUnderlyingError=0x14fde930 {Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x1607abf0> { URL: https://fcm.googleapis.com/fcm/send } { status code: 400, headers {
"Content-Length" = 54;
"Content-Type" = "text/html; charset=UTF-8";
Date = "Wed, 02 Nov 2016 13:24:20 GMT";
} }, NSErrorFailingURLKey=https://fcm.googleapis.com/fcm/send, NSLocalizedDescription=Request failed: unacceptable content-type: text/html, com.alamofire.serialization.response.error.data=<3c68746d 6c3e3c74 69746c65 3e457272 6f722034 30302028 42616420 52657175 65737429 2121313c 2f746974 6c653e3c 2f68746d 6c3e>}}}
这是我的代码:
NSDictionary *homeAddressDict = @{ @"message_from" : MessagesFrom, @"type" : typeOfMessage, @"time" : forDate, @"message" : sendMessges };
NSDictionary *params = @{@"data" : homeAddressDict, @"to":tofcmTokenID};
NSLog(@"%@",params);
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
// Content-type = "text/html";
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"text/plain"];
// manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
// "Content-Type" = "text/html;
//NSString *key = [self urlEncode:@"key=AIzaSyB6w3yutLPH2Tbr6HoQyynHssxUHVRt-Vc"];
[manager.requestSerializer setValue:@"key=AIzaSyBWr7ThxdSbyrJQjYHrQ2AvN9nUblh0ZGQ" forHTTPHeaderField:@"Authorization"];
manager.responseSerializer = [AFJSONResponseSerializer
serializerWithReadingOptions:NSJSONReadingAllowFragments];
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/plain"];
// manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager POST:@"https://fcm.googleapis.com/fcm/send" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Server groupssss JSON is: %@", responseObject);
NSLog(@"%@", operation.response.allHeaderFields);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", operation.response.allHeaderFields);
NSLog(@"Error: %@", error);
}];
【问题讨论】:
-
关键的错误信息是:unacceptable content-type: text/html。并且不要发布您的实际 API 密钥!
标签: ios objective-c afnetworking