objective-c - iOs 从 NSURLConnection 接收到的数据为零

标签 objective-c ios nsurlconnection

我想知道是否有人可以指出为什么我无法捕获网络回复。我的 NSLog 显示我的 [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

最佳答案

问题是您没有初始化 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");
    }
}

关于objective-c - iOs 从 NSURLConnection 接收到的数据为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13619114/

相关文章:

ios - 要求用户访问他们在地址簿中的联系人

ios - Swift UI 测试,如何检查单元格是否有 ImageView

ios - 在 iOS 中使用异步网络调用创建一个预先输入的 UISearchDisplay

iphone - iOS:我如何接收 HTTP 401 而不是 -1012 NSURLErrorUserCancelledAuthentication

php - 尝试移植 C++ 代码以将文件上传到服务器到 Objective-C,但它不起作用

iphone - 分析器报告以下代码泄漏

objective-c - UIBezierPath-带曲线的双向箭头

ios - Swift:for循环中的异步调用

ios - 以编程方式创建 UIImage 以在谷歌地图上显示标记?

objective-c - 从iPad状态栏中取出电池和 watch 图标