2011-10-03 22 views
8

मैं AFNetworking का उपयोग कर रहा हूं और एक पोस्ट अनुरोध तैयार कर रहा हूं जिसके लिए मुझे जेसन प्रतिक्रिया की आवश्यकता है। नीचे दिया गया कोड काम करता है हालांकि मेरे पास दो मुख्य प्रश्न हैं; मैं गतिविधिइंडिकेटर प्रबंधक कहां से जारी करूं? दूसरा सवाल यह कोड सही है, नया होने के कारण मैं ब्लॉक के साथ भ्रमित हो जाता हूं, इसलिए मैं वास्तव में जानना चाहता हूं कि मैं इसे सही प्रदर्शन के लिए सही काम कर रहा हूं, भले ही यह काम करता हो।जेसन प्रतिक्रिया के साथ AFNetworking पोस्ट अनुरोध

NSURL *url = [NSURL URLWithString:@"mysite/user/signup"]; 
    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; 

    AFNetworkActivityIndicatorManager * newactivity = [[AFNetworkActivityIndicatorManager alloc] init]; 
    newactivity.enabled = YES; 
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: 
          usernamestring, @"login[username]", 
          emailstring, @"login[email]", 
          nil]; 
    NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"mysite/user/signup"parameters:params]; 
    [httpClient release]; 

    AFJSONRequestOperation *operation = [AFJSONRequestOperation operationWithRequest:request success:^(id json) { 

     NSString *status = [json valueForKey:@"status"]; 
     if ([status isEqualToString:@"success"]) { 
      [username resignFirstResponder]; 
      [email resignFirstResponder]; 
      [self.navigationController dismissModalViewControllerAnimated:NO]; 
     } 
     else { 
      UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"Login Unsuccessful" 
                  message:@"Please try again" 
                  delegate:NULL 
               cancelButtonTitle:@"OK" 
               otherButtonTitles:NULL]; 

      [alert show]; 
      [alert release]; 
     } 

    } 

    failure:^(NSHTTPURLResponse *response, NSError *error) { 

    NSLog(@"%@", error); 
    UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"Login Unsuccessful" 
                 message:@"There was a problem connecting to the network!" 
                 delegate:NULL 
              cancelButtonTitle:@"OK" 
              otherButtonTitles:NULL]; 

     [alert show]; 
     [alert release]; 


    }]; 

    NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease]; 
    [queue addOperation:operation]; 
    NSLog(@"check");  


}  

आप पहले से आपकी मदद के लिए बहुत बहुत धन्यवाद :)

+0

कहां है 'AFJSONRequestOperation operationWithRequest: सफलता: खत्म:' विधि से आते हैं? मैं इसे एपीआई में नहीं देखता हूं। –

+0

@reakinator वह वास्तव में '+ JSONRequestOperationWithRequest का उल्लेख करता है: सफलता: विफलता:' उदाहरण देखें [यहां] (https://github.com/AFNetworking/AFNetworking#readme)। – borisdiakur

उत्तर

2

क्यों बजाय इसका उपयोग नहीं?

[[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES]; 

इसलिए alloc और init

कह नहीं सकते अन्य कोड पर ज्यादा, बस बाहर शुरू कर दिया सीखने उद्देश्य-सी और AFNetworking .. :)

सादर, Steve0hh की आवश्यकता नहीं है

8

मुझे पता है कि यह प्रश्न थोड़ा पुराना है, लेकिन मैं अभी भी योगदान देना चाहता हूं।

स्टीव ओह ने कहा, आपको गतिविधि नेटवर्क सूचक को चालू करने के लिए [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES] का उपयोग करना चाहिए। यह singleton है, और इसलिए आपको मैन्युअल रूप से आवंटित करने और रिलीज़ करने की आवश्यकता नहीं है। अन्य प्रश्न के रूप में, मैंने देखा है कि आप अपने ब्लॉक कॉल में कुछ मानकों याद कर रहे हैं, भी, आप जो अधिक स्वच्छ कोड है यह कर सकते हैं,:

NSURL *url = [NSURL URLWithString:@"mysite/user/signup"]; 
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:[NSURLRequest requestWithURL:url] success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 
    // your success code here 
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { 
    // your failure code here 
}]; 

[operation start]; // start your operation directly, unless you really need to use a queue 
+0

हैलो, मैंने आपका कोड इस्तेमाल किया लेकिन मुझे हमेशा विफलता ब्लॉक में मिला :(क्या मुझे कुछ याद आ रहा है? – Luca

+0

@Malek जांचें कि आपका यूआरएल सही है, मैं विफलता ब्लॉक से 'एनएसईआरआरआर' एनएसएलॉगिंग की सलाह देता हूं और आउटपुट देखता हूं। – ArturoVM

+0

@ मालेक यदि आप अभी भी इस मुद्दे को इंगित नहीं कर सकते हैं, तो उस NSLog के आउटपुट के साथ एक नया प्रश्न खोलें और मुझे आपकी मदद करने में खुशी होगी। – ArturoVM

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