2013-10-28 10 views
8

を:AFNetworking 2.0およびレスポンスのシリアライズオプション

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.} 

問題はどこでAFNetworking 2.0で断片を許可する設定すべきであるということですか?

UPDATE:

私のクラスはAFHTTPSessionManagerのサブクラスです。私はすでにJSONResponseSerializerを作成していますが、これも機能しません。次のように

JSONが返されている:

[{"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; 
} 

エラーは以下の通りであるが返さ:

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

WEBサーバからの応答:

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

応答ステータスFROMウェブサーバー:

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

Webサーバーの応答に問題があります。** AFNetworkingでは**ではありません。 – akashivskyy

+0

応答は有効です!私はJSONを検証しましたが、有効なJSONが返されています。 –

+0

私はそれが無効であるとは言いませんでした。私は*問題はそれによって引き起こされたと言った*。 JSONデータは、配列やオブジェクトに含まれる傾向があります。 @Wainが正しい場合は、AFNetworkingにJSONデータの読み込みにNSISSONSerializationが使用するオプションを指定する必要があります。 – akashivskyy

答えて

7

あなたはserializerWithReadingOptions:を使用してAFJSONResponseSerializerの独自のインスタンスを作成し、(デフォルトのJSON応答シリアライザの代わりに)それを使用するようにシステムを設定する必要があります。

+0

最新の質問をご覧ください!私はAFJSONResponseSerializerを追加しましたが、まだ使用していません! –

+0

JSONにフラグメントがあるように見えません。カスタムクラスが使用されていますか? – Wain

+0

...応答が破損していないか、他の何かの中に閉じ込められていますか? – akashivskyy

0

私は前に同じ問題がありました。数時間の検索と閲覧の後、私は何かを見つけました:

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

試してみてください!

関連する問題