2012-11-29 37 views
0

我想知道是否有人可以指出爲什麼我無法捕獲Web回覆。我的NSLog顯示我的[NSMutableData receivedData]長度爲整個連接運行的0。當我點擊登錄按鈕時,我點擊的腳本返回一個字符串。我的NSLog結果粘貼在下面,之後我粘貼了我的.h和.m文件。iOs從NSURLConnection收到的數據爲零

的NSLog結果

2012-11-28 23:35:22.083 [12548:c07] Clicked on button_login 
2012-11-28 23:35:22.090 [12548:c07] theConnection is succesful 
2012-11-28 23:35:22.289 [12548:c07] didReceiveResponse 
2012-11-28 23:35:22.290 [12548:c07] didReceiveData 
2012-11-28 23:35:22.290 [12548:c07] 0 
2012-11-28 23:35:22.290 [12548:c07] connectionDidFinishLoading 
2012-11-28 23:35:22.290 [12548:c07] 0 

ViewController.h

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController 

// Create an Action for the button. 
- (IBAction)button_login:(id)sender; 

// Add property declaration. 
@property (nonatomic,assign) NSMutableData *receivedData; 

@end 

ViewController.m

#import ViewController.h 

@interface ViewController() 
@end 

@implementation ViewController 

@synthesize receivedData; 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    NSLog(@"didReceiveResponse");  
    [receivedData setLength:0]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {  
    NSLog(@"didReceiveData");  
    [receivedData appendData:data]; 
    NSLog(@"%d",[receivedData length]); 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {  
    NSLog(@"connectionDidFinishLoading"); 
    NSLog(@"%d",[receivedData length]); 
} 

- (IBAction)button_login:(id)sender { 

    NSLog(@"Clicked on button_login");  
    NSString *loginScriptURL = [NSString stringWithFormat:@"http://www.website.com/app/scripts/login.php?"]; 

    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:loginScriptURL]]; 

    NSString *postString = [NSString stringWithFormat:@"&paramUsername=user&paramPassword=pass"]; 
    NSData *postData = [postString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

    [theRequest setHTTPMethod:@"POST"]; 
    [theRequest setHTTPBody:postData]; 

    // Create the actual connection using the request. 
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 

    // Capture the response 
    if (theConnection) {   
     NSLog(@"theConnection is succesful");   
    } else {   
     NSLog(@"theConnection failed"); 
    } 
} 
@end 

回答

4

問題是你沒有初始化receivedData實例。只要改變你的財產,如:

@property (nonatomic, retain) NSMutableData *receivedData; 

和變革,如方法:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    NSLog(@"didReceiveResponse");  
    [self.receivedData setLength:0]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{  
    NSLog(@"didReceiveData");  
    [self.receivedData appendData:data]; 
    NSLog(@"%d",[receivedData length]); 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{  
    NSLog(@"connectionDidFinishLoading"); 
    NSLog(@"%d",[receivedData length]); 
} 

- (IBAction)button_login:(id)sender 
{ 

    NSLog(@"Clicked on button_login");  
    NSString *loginScriptURL = [NSString stringWithFormat:@"http://www.website.com/app/scripts/login.php?"]; 

    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:loginScriptURL]]; 

    NSString *postString = [NSString stringWithFormat:@"&paramUsername=user&paramPassword=pass"]; 
    NSData *postData = [postString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

    [theRequest setHTTPMethod:@"POST"]; 
    [theRequest setHTTPBody:postData]; 

    // Create the actual connection using the request. 
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 

    // Capture the response 
    if (theConnection) 
    {   
     NSLog(@"theConnection is succesful"); 
     self.receivedData = [NSMutableData data];  
    } else 
    {   
     NSLog(@"theConnection failed"); 
    } 
} 
+0

仍然有問題? –

+0

.....我在過去兩個晚上一直在谷歌搜索和嘗試不同的東西。我花了30秒來申請你的更正,現在它返回一個長度。非常感謝你。 – yhl

+0

P.S. - 我想我需要一些不在互聯網上的閱讀材料(即一本書)。我需要學習很多東西。 – yhl

1

懇求在nslog中嘗試「%i」而不是%d

+0

爲什麼...我不是d ..,你試試ð,而不是我.. :) – Rajneesh071

1

您可以嘗試下面的代碼可能會對您有所幫助。

- (IBAction)button_login:(id)sender { 

    NSLog(@"Clicked on button_login"); 
      NSMutableDictionary *dictionnary = [NSMutableDictionary dictionary]; 
      [dictionnary setObject:@"user" forKey:@"Username"]; 
      [dictionnary setObject:@"pass" forKey:@"Password"]; 

      NSError *error = nil; 
      NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionnary 
                   options:kNilOptions 
                   error:&error]; 

      NSString *urlString = @"Sample URL"; 

      NSURL *url = [NSURL URLWithString:urlString]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
      [request setHTTPMethod:@"POST"]; 

      [request setHTTPBody:jsonData]; 
      NSURLResponse *response = NULL; 
      NSError *requestError = NULL; 
      NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError]; 
      NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] ; 
      NSLog(@"%@", responseString); 

} 

,如果它是一個GET請求,那麼,你可以嘗試鏈接:/login.php?username=admin &密碼= 1212 3

- (IBAction)button_login:(id)sender { 

    NSLog(@"Clicked on button_login"); 
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/login.php?username=adm‌​in&password=1212‌​3"]]; 

    // Perform request and get JSON as a NSData object 


    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
    NSLog(@"response=%@",response); 

} 

,並使用此代碼。

相關問題