2013-10-28 8 views
8

Appena ho attivare una richiesta di una risorsa JSON ottengo il seguente:AFNetworking 2.0 e la risposta opzioni di serializzazione

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=0x1d87a2c0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.} 

Il problema è che dove dovrei impostare il permettono frammenti in AFNetworking 2.0?

UPDATE:

La mia classe è una classe secondaria di AFHTTPSessionManager. Ho già creato un JSONResponseSerializer e anche questo non funziona.

JSON viene restituito è il seguente:

[{"StoryId":1,"Title":"The big red dog","Abstract":"There was a big red dog and the dog was very big","IsFeatured":true}] 

-(instancetype) initWithBaseURL:(NSURL *)url 
{ 
    NSURL *base = [NSURL URLWithString:@"URL to the web service that returns the json"]; 
    self = [super initWithBaseURL:base]; 

    AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments]; 

    [self setResponseSerializer:responseSerializer]; 

    return self; 
} 

L'errore riscontrato è la seguente:

[0] (null) @"NSDebugDescription" : @"Invalid value around character 0." 

RISPOSTA WEB SERVER:

[{"StoryId":1,"Title":"The big red dog","Abstract":"There was a big red dog and the dog was very big","IsFeatured":true}] 

RISPOSTA STATO DALLA SERVER WEB:

HTTP/1.1 200 OK 
Server: Microsoft-IIS/7.0 
X-Powered-By: ASP.NET 
Content-Type: application/json;charset=utf-8 
X-AspNetMvc-Version: 3.0 
Connection: close 
Date: Mon, 28 Oct 2013 20:02:21 GMT 
Content-Length: 121 
Cache-Control: private 
X-AspNet-Version: 4.0.30319 

[{"StoryId":1,"Title":"The big red dog","Abstract":"There was a big red dog and the dog was very big","IsFeatured":true}] 
+0

Questo è un problema con la risposta del server web, ** non ** con AFNetworking. – akashivskyy

+0

La risposta è valida! Ho appena convalidato il JSON ed è stato restituito JSON valido. –

+0

Non ho detto che è * non valido *. Ho detto che il * problema è causato da questo *. I dati JSON tendono ad essere contenuti all'interno di un array o di un oggetto. @Wain ha ragione, devi dire ad AFNetworking quali opzioni devono usare 'NSJSONSerialization' per leggere i dati JSON. – akashivskyy

risposta

7

È necessario creare il proprio istanza di AFJSONResponseSerializer utilizzando serializerWithReadingOptions: e configurare il sistema per utilizzarlo (al posto della risposta JSON serialiser default).

+0

Si prega di vedere la mia domanda aggiornata! Ho aggiunto AFJSONResponseSerializer ma ancora inutile! –

+0

Il tuo JSON non sembra avere frammenti. Sei sicuro che la tua classe personalizzata venga utilizzata? – Wain

+0

... e sei sicuro che la risposta non sia corrotta o racchiusa in qualcos'altro? – akashivskyy

0

Ho già avuto lo stesso problema. E dopo alcune ore di ricerca e lettura, ho trovato qualcosa:

self.responseSerializer = [AFHTTPResponseSerializer serializer]; self.responseSerializer.acceptableContentTypes = [NSSet setWithObjects: @ "application/json; charset = UTF-8", nil];

provalo e buona fortuna!

Problemi correlati