2012-03-06 14 views
9

मुझे NSURLConnection प्रतिनिधि विधि कार्यान्वयन के लिए कोई उदाहरण खोजने में कठिनाई हो रही है।NSURLConnection प्रतिनिधि विधि

मैं एक बटन क्लिक के साथ एक HTTP पोस्ट के साथ डेटा भेजना चाहता हूं। सुनिश्चित नहीं है कि "सबमिट करने" स्क्रीन और "सबमिट" कैसे करें। (मुझे पता है कि स्पिनर का उपयोग कैसे करें और उनका उपयोग कैसे करें)

मैं इस कोड का उपयोग कपास क्लिक एक्शन के तहत कर रहा हूं, लेकिन किसी भी प्रतिनिधि सामग्री का उपयोग करने में असमर्थ हूं। सुनिश्चित नहीं है कि मेरे वर्तमान सेट अप के साथ उन्हें कैसे कार्यान्वित किया जाए।

NSMutableURLRequest *request = 
    [[NSMutableURLRequest alloc] initWithURL: 
    [NSURL URLWithString:@"http://myURL.com"]]; 

    [request setHTTPMethod:@"POST"]; 

    NSString *postString = [wait stringByAppendingString:co]; 

    [request setValue:[NSString 
         stringWithFormat:@"%d", [postString length]] 
    forHTTPHeaderField:@"Content-length"]; 



    [request setHTTPBody:[postString 
          dataUsingEncoding:NSUTF8StringEncoding]]; 

    //[[NSURLConnection alloc] initWithRequest:request delegate:self]; 
    [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 


    [SVProgressHUD dismissWithSuccess:@"Submission Successful"]; 

उत्तर

8

आप नए NSURLConnectionDataDelegate प्रोटोकॉल का उपयोग करने की जरूरत है।

मैं कुछ exemples मिली:

http://blog.kemalkocabiyik.com/index.php/2012/02/fetching-data-with-getpost-methods-by-using-nsurlconnection/

और तुम पुर्तगाली पढ़ सकते हैं: इस कोड को आप GCD, गतिविधि संकेतक का उपयोग करेगा में http://www.quaddro.com.br/blog/desenvolvimento-ios/baixando-conteudo-com-nsurlconnection-e-nsurlconnectiondatadelegate-no-ios

+0

दूसरा लिंक, पोर्तुगीज वास्तव में मदद करता है, यहां तक ​​कि मुझे प्रोटूज समझ में नहीं आता है, धन्यवाद .. – Bhimbim

25
- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    NSLog(@"Did Receive Response %@", response); 
    responseData = [[NSMutableData alloc]init]; 
} 
- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data 
{ 
    //NSLog(@"Did Receive Data %@", data); 
    [responseData appendData:data]; 
} 
- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error 
{ 
    NSLog(@"Did Fail"); 
} 
- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    NSLog(@"Did Finish"); 
    // Do something with responseData 
} 
+0

यकीन नहीं कैसे करने के लिए सेटअप प्रतिनिधि, क्योंकि मैं में ऊपर मेरे सारे कोड पोस्ट किया है, - (IBAction) जमा: (आईडी) इस जिसे आप URL कनेक्शन आरंभ – socbrian

+0

सभी चार विधियों सक्रिय हो जाएगा। यह उसी वर्ग में कार्यान्वयन फ़ाइल में होना चाहिए जो कनेक्शन – Eric

+0

कनेक्शन शुरू करता है, तो यह कोड मेरी सबमिट कक्षा के तहत इस कोड को रखने के लिए कह रहा है? जब मैं ऐसा करता हूं तो मुझे त्रुटियां मिल रही हैं। – socbrian

4
//Connection request 
-(void)requestURL:(NSString *)strURL 
    { 
     // Create the request. 
     NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:strURL]]; 

     // Create url connection and fire request 
     NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

    } 


    //Delegate methods 
    - (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse *)response 
    { 
     NSLog(@"Did Receive Response %@", response); 
     responseData = [[NSMutableData alloc]init]; 
    } 
    - (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data 
    { 
     //NSLog(@"Did Receive Data %@", data); 
     [responseData appendData:data]; 
    } 
    - (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error 
    { 
     NSLog(@"Did Fail"); 
    } 
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection 
    { 
     NSLog(@"Did Finish"); 
     // Do something with responseData 

     NSString *strData=[[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding]; 

     NSLog(@"Responce:%@",strData); 
    } 

http://codewithchris.com/tutorial-how-to-use-ios-nsurlconnection-by-example/

2

, UI बटन बटन लॉगिन बटन पर सबसे पहले आप wil मैं किसी अन्य थ्रेड पर StartActivityindicator को कॉल करता हूं और यह तब तक चलता रहता है जब तक आप गतिविधिइंडिकेटर को हटा या बंद नहीं करते। तो आप जीसीडी कतार में लॉगिन के लिए वेब सेवा कॉल करेंगे। यूआई को अपडेट करने के लिए आपको सर्वर कॉल मुख्य कतार से प्रतिक्रिया प्राप्त होने पर ।

// After the interface declration 
@interface LoginViewController() 
{ 

NSData *responseData; 
dispatch_queue_t myqueue; 

}  
//Button Action 
- (IBAction)Login_Button_Action:(id)sender 

{ 

[NSThread detachNewThreadSelector: @selector(StartActivityindicator) toTarget:self withObject:nil]; 
myqueue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0); 
dispatch_group_t group=dispatch_group_create(); 
dispatch_group_async(group, myqueue, ^{ [self loginWebService];}); 
} 
-(void)loginWebService 
{ 
//Combine Both url and parameters 
    NSString *UrlWithParameters = [NSString stringWithFormat:@"http://www.xxxxx.com?count=%@&user=%@&email=%@&password=%@",@"4",@"Username",[email protected]"UserEmail",@"PAssword String"]; 
//Pass UrlWithParameters to NSURL 
NSURL *ServiceURL =[NSURL URLWithString:UrlWithParameters]; 

NSMutableURLRequest *serviceRequest =[NSMutableURLRequest requestWithURL:ServiceURL]; 
[serviceRequest setHTTPMethod:@"POST"]; 

[serviceRequest setValue:@"application/json" forHTTPHeaderField:@"accept"]; 
[serviceRequest setValue:@"application/json" forHTTPHeaderField:@"content-type"]; 

//GEt Response Here 
NSError *err; 
NSURLResponse *response; 
responseData = [NSURLConnection sendSynchronousRequest:serviceRequest returningResponse:&response error:&err]; 

NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response; 
NSInteger code = [httpResponse statusCode]; 
// check status code for response from server and do RND for code if you recive anything than 200 
NSLog(@"~~~~~ Status code: %ld",(long)code); 
if (code ==200) 
{ 
// your response is here if you call right 
    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err]; 

dispatch_async(dispatch_get_main_queue(),^{ 
     // place the code here to update UI with your received response 
     [NSThread detachNewThreadSelector: @selector(StopActivityindicator) toTarget:self withObject:nil]; 
     }); 
} 
} 
//Activity indicator Method to display 
- (void) StartActivityindicator 
{ 
mySpinner.hidden = NO; 
[mySpinner startAnimating]; 
} 
- (void) StopActivityindicator 
{ 
mySpinner.hidden = YES; 
[mySpinner stopAnimating]; 
} 
+0

कृपया इस कोड का क्या विवरण है इसके बारे में एक संक्षिप्त विवरण जोड़ें। भी, समीक्षा करने के लिए एक पल लें [उत्तर] –

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