2014-02-05 54 views
1

我有一個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.... 
} 
+0

順便說一句,你是用'CFURLCreateStringByAddingPercentEscapes'轉義''bodyData'中捕獲的值嗎?另外,如果'bodyData'是一個字符串,'[request setHTTPBody:[bodyData dataUsingEncoding:NSUTF8StringEncoding]]'。 – Rob

+0

是的,我正在那樣做,只是沒有在上面顯示它,因爲我不認爲這是必要的。我確信服務器正在成功接收我的請求數據。 – user1775671

回答

0

你應該看看的響應和診斷髮生了什麼事情。例如,如果statusCode不是200,則可能有問題。請參閱HTTP/1.1 Status Code Definitions

所以,你可能檢查statusCode

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    if ([response isKindOfClass:[NSHTTPURLResponse class]]) { 
     NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode]; 

     if (statusCode == 200) { 
      NSLog(@"received successful (200) response "); 
     } else { 
      NSLog(@"whoops, something wrong, received status code of %d", statusCode); 
     } 
    } else { 
     NSLog(@"Not a HTTP response"); 
    } 
} 

你還呼籲:

theconnection = [NSURLConnection connectionWithRequest:request delegate:self]; 

自動啓動連接。致電

[theconnection start]; 

您正在第二次啓動它。刪除start方法,或使用initWithRequest:delegate:startImmediately:NO作爲最終參數。

+0

好的,謝謝,我的狀態碼爲200 – user1775671

+0

@ user1775671非常好。嘗試刪除多餘的「開始」方法,看看是否可以糾正這種情況。 – Rob

+0

是的,我試過了。我無法想象這是一個很大的問題。這不會讓它發送兩次請求嗎? – user1775671

0

如果它可以幫助任何人,對我來說,問題是,我開始在另一個線程的連接

所以一定要打電話

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

theconnection = [NSURLConnection connectionWithRequest:request delegate:self]; 
[theconnection start]; 

在主線程。