获取ASIFormDataRequest响应后应用程序崩溃

时间:2012-09-18 06:52:54

标签: objective-c ios asiformdatarequest

在我的应用程序中,我使用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]; 

2 个答案:

答案 0 :(得分:2)

[request setDelegate:self];
[request startAsynchronous];
NSThread sleepForTimeInterval:5];
NSLog(@"response %@",[request responseString]);
registrationStatusBlock =[[request responseString]JSONValue]; 
那是什么?你启动一个异步请求,然后睡觉,然后醒来,好像你得到了一个回应? 这不是异步的工作原理。

答案 1 :(得分: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]);
}