我有一個NSURLConnection已經工作了一段時間,突然間沒有工作。NSURLConnection不返回數據
出於某種原因,被調用的唯一的委託方法是:
-(void)connection:(NSURLConnection *)connection didReceiveResponse:
的其他委託方法沒有被調用。我有其他類使用幾乎相同的代碼,只是一個不同的請求和網址,他們似乎都工作得很好。我已經閱讀了大量的帖子,談論確保連接與委託人在同一個線程等,但似乎沒有爲我工作。我知道服務器正在返回響應,因爲如果我通過簡單的HTML表單傳遞相同的信息,我會在瀏覽器中得到響應,並且我可以看到證據顯示我的服務器端腳本正在運行,因爲我可以看到它正在進行的更改SQL數據。該應用程序正在得到某種反應,它只是沒有獲取任何數據或調用connectionDidFinishLoading委託方法。
任何想法可能是什麼問題?
這裏是我的代碼的簡化版本:
#import "RegistrationViewController.h"
@interface RegistrationViewController()
@end
NSMutableData *responseData;
NSURLConnection *theconnection;
@implementation RegistrationViewController
/*
* Attempt to Register online. Returns False if valiation failed
*/
- (BOOL) registerOnline{
// OTHER CODE HERE TO BUILD DATA FOR THE REQUEST
// URL Request
NSURL *requestUrl = [NSURL URLWithString:THEURL];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:requestUrl];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[NSData dataWithBytes:[bodyData UTF8String] length:strlen([bodyData UTF8String])]];
// Initialse Response Object
responseData = [[NSMutableData alloc] init];
// Conection
theconnection = [NSURLConnection connectionWithRequest:request delegate:self];
[theconnection start];
return YES;
}
/*
* Handle the event of registration failing
*/
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"didFailWithError");
// OTHER CODE HERE TO HANDLE THE ERROR....
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSLog(@"did receive response ");
}
/*
* Handle the reciept of Data
*/
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
NSLog(@"didReceiveData");
// Add data to the response
[responseData appendData:data];\
}
/*
* Data finnished loading
*/
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"connectionDidFinishLoading");
// OTHER CODE HERE TO HANDLE THE RESPONSE....
}
順便說一句,你是用'CFURLCreateStringByAddingPercentEscapes'轉義''bodyData'中捕獲的值嗎?另外,如果'bodyData'是一個字符串,'[request setHTTPBody:[bodyData dataUsingEncoding:NSUTF8StringEncoding]]'。 – Rob
是的,我正在那樣做,只是沒有在上面顯示它,因爲我不認爲這是必要的。我確信服務器正在成功接收我的請求數據。 – user1775671