AFNetworking accept content type error: {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

13,592

Solution 1

Man, this is what I added in when I config my AFNetworking class and it works fine.

The reason you got that 3840 error, is because your API server returns an integer or even empty stuff, which ios json parser failed.

Can u make sure in swift, the phrase your are using is correct way?

_delegateClient.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];

Solution 2

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFJSONResponseSerializer
serializerWithReadingOptions:NSJSONReadingAllowFragments];

[manager GET:url parameters:parameters progress:nil

         success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
             NSLog(@"request: %@\n", responseObject);
             completionBlock(responseObject,nil);
         }
         failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
             NSLog(@"Error: %@\n", error);
             completionBlock(nil,error);
         }];
Share:
13,592

Related videos on Youtube

William Hu
Author by

William Hu

10 years mobile development experiences.

Updated on May 29, 2022

Comments

  • William Hu
    William Hu about 2 years

    I am using AFNetworking to GET a plain/text:

        let manager = AFHTTPRequestOperationManager()
    
        manager.GET(url, parameters: nil, success: { (op: AFHTTPRequestOperation!, res: AnyObject!) -> Void in
    
            },failure: { (op: AFHTTPRequestOperation!, er:NSError!) -> Void in
            println(op,er)
    
        })
    

    The accept content types:

    manager.responseSerializer.acceptableContentTypes
    [text/javascript, application/json, text/json]
    

    So I got error in failure block:

    NSLocalizedDescription=Request failed: unacceptable content-type: text/plain}
    

    Then i added text/plain in this way:

    var set = manager.responseSerializer.acceptableContentTypes
    set.insert("text/plain")
    manager.responseSerializer.acceptableContentTypes = set
    

    right now the types are:

    manager.responseSerializer.acceptableContentTypes
    [application/json, text/javascript, text/plain, text/json]
    

    But i got the new error:

    { URL: http://192.168.1.9:8081/sec.jsp } { status code: 200, headers {
    "Content-Length" = 44;
    "Content-Type" = "text/plain; charset=utf-8";
    Expires = "Thu, 01 Jan 1970 00:00:00 GMT";
    Server = "Jetty(6.1.10)";
    "Set-Cookie" = "JSESSIONID=tapmhct7hanv;Path=/";
    } }>, Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x7fda31fb4a90 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.})
    

    I think the question is because JSON can't read the response. But i have set it to text/plain, does it still try to parse the plain text as JSON?

    I searched and try a way:

    manager.responseSerializer = AFJSONResponseSerializer(readingOptions: NSJSONReadingOptions.AllowFragments)
    

    But the error is:

    Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Garbage at end.) UserInfo=0x7ff4b1790660 {NSDebugDescription=Garbage at end.})
    
  • William Hu
    William Hu almost 9 years
    Hey man, manager.responseSerializer = AFJSONResponseSerializer(readingOptions: NSJSONReadingOptions.AllowFragments) i think what i tried is the same as yours. Just does't work.
  • Włodzimierz Woźniak
    Włodzimierz Woźniak almost 8 years
    You should try AFHTTPResponseSerializer (instead AFJSONResponseSerializer). It works for me