objective-ciosasiformdatarequest

Application crash after getting ASIFormDataRequest response


In my app i am using ASIFormDataRequest to send my new user registration request . That request contains user details along with image that is converted using base64Encoding. I got the response also. but the application get crash after getting the response. please help me to fix this issue. is there any error is this code?

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]; 

Solution

  • Use the delegate to get the response string.

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

    Then capture the response from the delegate:

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