错误:错误域 = NSCocoaErrorDomain 代码 = 3840“字符 0 周围的值无效。” UserInfo=NSDebugDescription=字符 0 周围的值无效。

Posted

技术标签:

【中文标题】错误:错误域 = NSCocoaErrorDomain 代码 = 3840“字符 0 周围的值无效。” UserInfo=NSDebugDescription=字符 0 周围的值无效。【英文标题】: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 【问题描述】:

我正在使用 AFNetworkingios 中使用 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 密钥! 【参考方案1】:

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

【讨论】:

以上是关于错误:错误域 = NSCocoaErrorDomain 代码 = 3840“字符 0 周围的值无效。” UserInfo=NSDebugDescription=字符 0 周围的值无效。的主要内容,如果未能解决你的问题,请参考以下文章

错误:错误域=NSURLErrorDomain 代码=-1001“请求超时。”

winserver2012设置域控后提示密码错误

跨域错误是怎么回事

Json 错误:错误域 = NSCocoaErrorDomain 代码 = 3840“无值”。 UserInfo=NSDebugDescription=无值。

错误 - 跨域请求错误

iOS:- 错误:无法创建存储目录。错误域 = NSCocoaErrorDomain 代码 = 512