简体   繁体   English

得到ASIFormDataRequest响应后,应用程序崩溃

[英]Application crash after getting ASIFormDataRequest response

In my app i am using ASIFormDataRequest to send my new user registration request . 在我的应用程序中,我正在使用ASIFormDataRequest发送新的用户注册请求。 That request contains user details along with image that is converted using base64Encoding. 该请求包含用户详细信息以及使用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]; 
[request setDelegate:self];
[request startAsynchronous];
NSThread sleepForTimeInterval:5];
NSLog(@"response %@",[request responseString]);
registrationStatusBlock =[[request responseString]JSONValue]; 

What is that? 那是什么? You start an async request and then sleep and then wake up as if you got a response? 您启动了一个异步请求,然后进入睡眠状态,然后好像得到了响应一样醒来? That is not how async works. 异步不是这样工作的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM