2012-04-17 70 views
2

這又是一個簡單的問題who'se答案在谷歌一個小時後,我想不起來了...NSURLConnection的返回數據,但我不能讀它

一個用戶鍵入的東西到一個文本字段,並在這一領域失去焦點,我推出一個NSURLConnection的....

NSString *usernameTry = [sender text]; 
NSLog(@"username field = %@",usernameTry); 

NSString *content = [@"http://www.siebenware.de/Rhythm/user/checkusername.php?username=" stringByAppendingString:usernameTry]; 
content = [content stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; 

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:content] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:5.0]; 
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
[connection start]; 
NSLog(@"%@",content); 

然後我等待響應....

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
NSLog(@"Received Response"); 
if ([response isKindOfClass:[NSHTTPURLResponse class]]) { 
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response; 
    int status = [httpResponse statusCode]; 

    if (!((status >= 200) && (status < 300))) { 
     NSLog(@"Connection failed with status %@", status); 
     [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 
    } 
} 

} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
[resultsData appendData:data]; 
NSString *resultsString = [[NSString alloc] initWithData:resultsData encoding:NSUTF8StringEncoding]; 
NSLog(@"received data %@ %i",resultsString,[resultsString length]); 

if ([resultsString isEqualToString: @"FAIL"]){ 
    NSLog(@"failed to retrieve data"); 
}else{  
    resultsObjects = [[resultsString componentsSeparatedByString:@":"] mutableCopy]; 

    if([resultsObjects objectAtIndex:0][email protected]"usernamecheck"){ 
     if ([resultsObjects objectAtIndex:1]==username.text) { 
      if ([resultsObjects objectAtIndex:2][email protected]"NG"){ 
       //set the check marker to bad 
       [usernameVer setImage:[UIImage imageNamed:@"redcross.png"]]; 
      }else{ 
       //set the check market to good 
       [usernameVer setImage:[UIImage imageNamed:@"greentick.png"]]; 
      } 
     } 

    } 
} 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 

NSLog([NSString stringWithFormat:@"Connection failed: %@", [error description]]); 
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Unable to retrieve data" message:[NSString stringWithFormat:@"Error code %i",[error code]] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 

} 

我看到響應在didReceiveResponse到達,我得到18個字節的數據(這是我s正確)。

然而didReceiveData說,「數據」爲空。我知道我錯過了一些非常簡單的事情,並且我保證一旦清理完畢就會踢自己。

問候 克里斯^ h

+0

你究竟看到了18字節的數據? – giorashc 2012-04-17 11:14:01

+0

對不起,刪除了代碼。在didReceiveResponse我以前看過我收到了多少,但一旦它工作,我刪除了該行代碼。 – 2012-04-17 11:15:20

+0

當你說data = null時,你的意思是數據參數或你的resultsData成員? – giorashc 2012-04-17 11:17:07

回答

5
如果你想將數據轉變成一個NSString你可以這樣做

NSString *responsestring = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
NSLog(@"%@", responsestring); 
+0

感謝您的指針。我不明白,但現在如果我不刪除[responseData append:data]並將initWithData:responseData更改爲initWithData:data,它現在可以工作。 – 2012-04-17 11:22:20

+0

很高興聽到你想通了! – Manuel 2012-04-17 11:23:59

+0

對不起,意思是如果我刪除append:data行並更改initwithData直接訪問'data'對象: – 2012-04-17 13:48:08

0

與其他encoding..May嘗試是ASCII編碼,而不是UTF-8。 。它可能會制定出

+0

謝謝,但是我在服務器上強制UTF8。 – 2012-04-17 13:46:30

1

您需要使用您的數據的工作:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection; 

因爲

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data; 

可以調用多次

你可以這樣做:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [myData appendData:data]; 
} 
- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    // TODO: work with data 
} 

NSMutableData * myData的必須實例變種。

1

您需要使用下面的代碼

 
NSString *finalString = [[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding]; 

由NSURLConnection的委託方法,以可讀的NSString收到的NSMutableData轉換並應該這樣做。

相關問題