2013-10-28 11 views
8

जैसे ही मैंने एक JSON संसाधन मैं निम्नलिखित प्राप्त करने के लिए एक अनुरोध को ट्रिगर: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 में टुकड़े के लिए अनुमति देते है?

अद्यतन:

मेरी कक्षा AFHTTPSessionManager की एक उप वर्ग है। मैंने पहले ही एक JSONResponseSerializer बनाया है और यह भी काम नहीं करता है।

इस प्रकार

JSON लौटे जा रहा है:

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

वेब सर्वर से प्रतिक्रिया:

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

प्रतिसाद स्थिति से

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

त्रुटि दी पीछा कर रहा है वेब सर्वर:

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

यह वेब सर्वर की प्रतिक्रिया के साथ एक समस्या है, ** ** नहीं, AFNetworking के साथ। – akashivskyy

+0

प्रतिक्रिया मान्य है! मैंने बस जेएसओएन को मान्य किया है और यह वैध JSON लौटाया जा रहा है। –

+0

मैंने यह नहीं कहा कि यह * अमान्य * है। मैंने कहा कि * समस्या इसके कारण होती है *। JSON डेटा किसी सरणी या ऑब्जेक्ट के भीतर निहित होता है। @Wain सही है, आपको AFNetworking को बताना होगा कि JSON डेटा को पढ़ने के लिए कौन से विकल्प 'NSJSONSerialization' उपयोग करना चाहिए। – akashivskyy

उत्तर

7

आप serializerWithReadingOptions: का उपयोग कर AFJSONResponseSerializer का अपना स्वयं का उदाहरण बना सकते हैं और इसका इस्तेमाल करने के लिए (डिफ़ॉल्ट JSON उत्तर serialiser के स्थान पर) आपके सिस्टम विन्यस्त करने की जरूरत।

+0

कृपया मेरा अपडेट किया गया प्रश्न देखें! मैंने AFJSONResponseSerializer जोड़ा लेकिन अभी भी कोई उपयोग नहीं है! –

+0

आपका JSON ऐसा नहीं लगता है कि इसमें टुकड़े हैं। क्या आप सुनिश्चित हैं कि आपकी कस्टम कक्षा का उपयोग किया जा रहा है? – Wain

+0

... और क्या आप सुनिश्चित हैं कि प्रतिक्रिया किसी और के भीतर भ्रष्ट या संलग्न नहीं है? – akashivskyy

0

मुझे पहले भी यही समस्या मिली। और कुछ घंटों के बाद खोज और पढ़ने के बाद, मुझे कुछ मिला:

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

इसे और शुभकामनाएं आजमाएं!

संबंधित मुद्दे