我想知道是否有人可以指出为什么我无法捕获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:@"¶mUsername=user¶mPassword=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
仍然有问题? –
.....我在过去两个晚上一直在谷歌搜索和尝试不同的东西。我花了30秒来申请你的更正,现在它返回一个长度。非常感谢你。 – yhl
P.S. - 我想我需要一些不在互联网上的阅读材料(即一本书)。我需要学习很多东西。 – yhl