2016-11-02 136 views
0

我使用AFNetworking使用的iOS後的方法來從服務器獲取數據,但我得到錯誤:錯誤域= NSCocoaErrorDomain代碼= 3840「字符0周圍的值無效。」的UserInfo = {NSDebugDescription =無效字符周圍值0}

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); 

    }]; 
+1

關鍵的錯誤信息是:**不可接受的內容類型:text/html **。並且不要發佈您的實際API密鑰! – vadian

回答

0

您可以使用'AFHTTPResponseSerializer'設置'manager.responseSerializer.acceptableContentTypes',您可以在其中設置內容類型。

相關問題