2012-09-18 66 views
0

在我的應用程序中,我使用ASIFormDataRequest發送我的新用戶註冊請求。該請求包含用戶詳細信息以及使用base64Encoding轉換的圖像。我也得到了答覆。但應用程序在獲得響應後會崩潰。請幫我解決這個問題。這個代碼有什麼錯誤嗎?獲取ASIFormDataRequest響應後的應用程序崩潰

NSString *[email protected]"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 
      [email protected]""; 

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

崩潰日誌? http://emclstcd.tk – 2012-09-18 06:53:44

+0

崩潰日誌和調用堆棧說什麼?你知道拋出異常的位置嗎? –

+0

這是一條完全不請自來的建議 - 但您應該認識到ASIHTTPRequest [不再處於開發中](http://allseeing-i.com/%5Brequest_release%5D),開發人員建議其他開發人員尋找替代方案。如果這是一個新項目,並且您正在尋找異步網絡代碼 - AFNetworking就是其中一種選擇。還有其他人。 – FluffulousChimp

回答

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]); 
} 
2
[request setDelegate:self]; 
[request startAsynchronous]; 
NSThread sleepForTimeInterval:5]; 
NSLog(@"response %@",[request responseString]); 
registrationStatusBlock =[[request responseString]JSONValue]; 

那是什麼?你開始一個異步請求,然後睡覺,然後醒來,就好像你有迴應? 這不是異步工作。