0

在我的应用程序中,我使用 ASIFormDataRequest 发送我的新用户注册请求。该请求包含用户详细信息以及使用 base64Encoding 转换的图像。我也得到了回应。但应用程序在收到响应后崩溃。请帮我解决这个问题。这段代码有什么错误吗?

NSString *sx=@"male";
        registrationStatusBlock = response;
        NSDateFormatter *dateOfBirthFormatter = [[NSDateFormatter alloc] init];
        [dateOfBirthFormatter setDateFormat:@"YYYY:MM:dd"];
        [dateOfBirthFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
        NSString *formattedDate = [dateOfBirthFormatter stringFromDate:userDetails.dateOfBirth];


        NSLog(@"URL: %@", [NSString stringWithFormat:@"%@&firstName=%@&lastName=%@&password=%@&emailId=%@&portrait=%@&dob=%@&gender=%@&task=createUser",webServiceURL,userDetails.firstName, userDetails.lastName, userDetails.emailAddress, userDetails.password, userDetails.profilePicture, formattedDate, sx]);
        NSLog(@"Gender %@",userDetails.gender);

        int lengthOfData=0;
        NSString *encodedString ;
        NSData *    imageData = UIImagePNGRepresentation(userDetails.profilePicture);

        registrationStatusBlock = response;
        self.responseData = [NSMutableData data];
        if(userDetails.profilePicture !=NULL)
        {
            lengthOfData = imageData.length;
            encodedString =  [NSString base64StringFromData:(NSData *) imageData length:lengthOfData];
        } 
        else
            encodedString=@"";

        NSLog(@"URL: %@", [NSString stringWithFormat:@"%@&firstName=%@&lastName=%@&password=%@&emailId=%@&portrait=%@&portrait_type=png&dob=%@&gender=%@&task=createUser",webServiceURL,userDetails.firstName, userDetails.lastName, userDetails.emailAddress, userDetails.password, encodedString, formattedDate, sx]);



        NSMutableString *URLString = [[NSMutableString alloc]initWithString:webServiceURL];
        [URLString appendString:[NSString stringWithFormat:@"%@&firstName=%@&lastName=%@&password=%@&emailId=%@&portrait=%@&portrait_type=png&dob=%@&gender=%@&task=createUser",webServiceURL,userDetails.firstName, userDetails.lastName, userDetails.emailAddress, userDetails.password, encodedString, formattedDate, sx]];

        ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:[webServiceURL stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]];

        [request addPostValue:userDetails.firstName       forKey:@"firstName"];
        [request addPostValue:userDetails.lastName        forKey:@"lastName"];
        [request addPostValue:userDetails.password        forKey:@"password"];
        [request addPostValue:userDetails.emailAddress    forKey:@"emailId"];
        [request addPostValue:encodedString forKey:@"portrait"];
        [request addPostValue:@"png" forKey:@"portrait_type"];

        [request addPostValue:formattedDate forKey:@"dob"];

        [request addPostValue:sx forKey:@"gender"];

        [request addPostValue:@"createUser"                   forKey:@"task"];
        [request setRequestMethod:@"POST"];
        NSLog(@" user registration request %@",request);
        [request setDelegate:self];
        [request startAsynchronous];
        [NSThread sleepForTimeInterval:5];
        NSLog(@"response %@",[request responseString]);
        registrationStatusBlock =[[request responseString]JSONValue]; 
4

2 回答 2

2
[request setDelegate:self];
[request startAsynchronous];
NSThread sleepForTimeInterval:5];
NSLog(@"response %@",[request responseString]);
registrationStatusBlock =[[request responseString]JSONValue]; 

那是什么?你开始一个异步请求,然后睡觉,然后像得到响应一样醒来?这不是异步的工作方式。

于 2012-09-18T07:51:25.460 回答
1

使用委托获取响应字符串。

[request setDelegate:self];
[request setDidFinishSelector:@selector(requestFinished:)];
[request setDidFailSelector:@selector(requestFailed:)];
[request startAsynchronous];

然后捕获委托的响应:

- (void)requestFinished:(ASIHTTPRequest *)theRequest {
     NSLog(@"response %@",[theRequest responseString]);
}

- (void)requestFailed:(ASIHTTPRequest *)theRequest {
     NSLog(@"response Failed%@, Error:%@",[theRequest responseString],[theRequest error]);
}
于 2012-09-18T08:58:04.193 回答