首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >来自NSURLConnection的iOs NSURLConnection为零

来自NSURLConnection的iOs NSURLConnection为零
EN

Stack Overflow用户
提问于 2012-11-29 04:57:52
回答 2查看 1.6K关注 0票数 0

我想知道是否有人能指出为什么我不能捕捉到一个网络回复。我的NSLog显示,在连接的整个运行过程中,[NSMutableData receivedData]的长度为0。单击登录按钮时单击的脚本将返回一个字符串。我的NSLog结果粘贴在下面,之后我粘贴了我所拥有的.h和.m文件。

NSLog结果

代码语言:javascript
复制
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

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-11-29 05:05:08

问题是您没有初始化receivedData实例。只要改变你的财产,就像:

代码语言:javascript
复制
@property (nonatomic, retain) NSMutableData *receivedData;

并更改方法,如:

代码语言:javascript
复制
- (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");
    }
}
票数 4
EN

Stack Overflow用户

发布于 2012-11-29 05:03:23

您可以尝试下面的代码可能对您有所帮助。

代码语言:javascript
复制
- (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吗?

代码语言:javascript
复制
- (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 ); 

}

并使用这段代码。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13619114

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档