【问题标题】:Error: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}错误:错误域 = NSCocoaErrorDomain 代码 = 3840“字符 0 周围的值无效。” UserInfo={NSDebugDescription=字符 0 周围的值无效。}
【发布时间】: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


【解决方案1】:

您可以使用“AFHTTPResponseSerializer”设置“manager.responseSerializer.acceptableContentTypes”,您可以在其中设置内容类型。

【讨论】:

    猜你喜欢
    • 2018-02-20
    • 1970-01-01
    • 2016-09-19
    • 1970-01-01
    • 2017-06-24
    • 2018-06-17
    • 2014-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多