2012-01-23 10 views
0

私は自分のWebサイトから私が解析する必要があるデータを取得する私のiPhoneに接続を作成したいと思います。私は、これまでの任意の運を持って、次のデリゲートメソッドに関して一種の混乱していないのです。URL接続ios

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data 

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 

-(void)connectionDidFinishLoading:(NSURLConnection *)connection 

これらのメソッドは、私のコードそこから自分に求めているか、私はそれらを手動で呼び出す必要がありますか? .hファイルのどこにでもデリゲートを宣言する必要がありますか? これは私がやっていることですが運がなかったことです。誰かがそれを理解できればそれは高く評価されるだろう。私の接続は成功したが、NSLogはdidFailWithErrorのコンソールに表示されます。

おかげ

-(void) data: (id) sender 
{ 
    NSString *stringToBeSent; 
    NSURL *siteWithNumbers; 
    NSString *translation; 
    NSError *error; 
    NSString *boo; 

    sender= [sender lowercaseString]; 
    sender= [sender stringByReplacingOccurrencesOfString:@"," withString:@""]; 


    receivedData= [[NSMutableData alloc] init]; //declared in .h file as NSMutableData 

    stringToBeSent= [[NSString alloc] 
    initWithFormat:@"http://xxxx/sql.php? data=%@",sender]; 

    NSURLRequest *theRequest=[NSURLRequest 
     requestWithURL:[NSURL URLWithString:stringToBeSent]]; 
    NSURLConnection *conn= [[NSURLConnection alloc] 
     initWithRequest:theRequest delegate:self]; 

    //[self createConnectionWithPath:stringToBeSent]; 

    if(conn) 
    { 
     NSLog(@"Connection Successful"); 
    } 
    else 
    { 
     NSLog(@"Connection could not be made"); 
    } 
} 


- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    /* appends the new data to the received data */ 
    NSLog(@"here now1"); 
    [self.receivedData appendData:data]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)conn 
{ 
    NSString *stringData= [[NSString alloc] 
         initWithData:receivedData encoding:NSUTF8StringEncoding]; 
    NSLog(@"Got data? %@", stringData); 

    [conn release]; 
    conn = nil; 
} 

- (void)connection:(NSURLConnection *) 
    connection didFailWithError:(NSError *)error 
{ 
    NSLog(@"fail"); 
} 
+1

デリゲートメソッドは、NSURLConnectionによって呼び出されます。手動で呼び出すことはありません。 'if(conn)'は、NSURLConnectionオブジェクトが正常に作成されたことを意味し、接続が「作成され、データが正常に送受信された」ことを意味しません。 didFailWithErrorのエラーを記録すると、問題の内容を知ることができます: 'NSLog(@" fail、error =%@ "、error);' – Anna

答えて

1
//in .h file  
    @interface yourViewController : UIViewController<NSURLConnectionDelegate> 
     { 
      NSMutableData *responseData; 
     } 

    // in .m file 

-(void) data: (id) sender 
{ 
NSString *strWithURL = [NSString stringWithFormat:@"%@%@",TownsServiceURL,state]; 

    strWithURL = [strWithURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
    NSLog(@"strConfirmChallenge=%@",strWithURL); 

    NSURL *myURL = [NSURL URLWithString:strWithURL]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL 
                  cachePolicy:NSURLRequestReloadIgnoringLocalCacheData 
                 timeoutInterval:60]; 

    [NSURLConnection connectionWithRequest:request delegate:self]; 
} 


//Delegate methods 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    responseData = [[NSMutableData alloc] init]; 

} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [responseData appendData:data]; 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 

    NSLog(@"Connection failed with error: %@", [error localizedDescription]); 


    UIAlertView *ConnectionFailed = [[UIAlertView alloc] 
            initWithTitle:@"Connection Failed" 
            message: [NSString stringWithFormat:@"%@", [error localizedDescription]] 
            delegate:self 
            cancelButtonTitle:@"Ok" 
            otherButtonTitles:nil]; 
    [ConnectionFailed show]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    NSString *s = [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding]; 

}