0

Web の返信をキャプチャできない理由を誰かが指摘できるかどうか疑問に思っていました。MyNSLogは、my[NSMutableData receivedData]の長さが接続全体で 0 であることを示しています。ログイン ボタンをクリックしたときにヒットしたスクリプトは、文字列を返します。私のNSLog結果は下に貼り付けられ、その後、私が持っている .h ファイルと .m ファイルの両方を貼り付けました。

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

3 に答える 3

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");
    }
}
于 2012-11-29T05:05:08.703 に答える
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&password=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 ); 

}

このコードを使用します。

于 2012-11-29T05:03:23.000 に答える
1

nslog で %d の代わりに「%i」を試してください

于 2012-11-29T05:00:50.893 に答える