2012-03-10 2 views
1

요청시 YES 또는 NO를 반환하는 PHP 웹 사이트가 있습니다. 예를 들어 Objective-C에서 PHP 웹 사이트 응답 받기

www.test.com/test.php?variable=test1 YES 저를 반환하는 것은 및 www.test.com/test.php?variable=test2 나에게 NO

I를 반환하지 않습니다 이 응답을 Objective-C 애플리케이션에 넣으려고 시도하고 있지만 지금까지는 운이 없다. 여기 내 코드

NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.test.com/test.php?variable=test1"] 
              cachePolicy:NSURLRequestUseProtocolCachePolicy 
             timeoutInterval:60.0]; 
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 
if (theConnection) { 
    NSMutableData *receivedData = [NSMutableData data]; 
    NSString *strData = [[NSString alloc]initWithData:receivedData encoding:NSUTF8StringEncoding]; 
    NSLog(@"This is the response: %@", strData); 
}else { 
} 

아무도 도와 줄 수 있습니까? 그것을 할 수있는 다른 방법이 있습니까? 내가 뭔가 잘못하고 있는거야?

고마워 많은 사람들

+0

"지금까지 행운이 없다"고 할 때, 무슨 일이 일어나고있는 걸까요? 크래시 "응답입니다."절대로 인쇄하지 않습니까? 델리게이트가 데이터를 수신하도록 설정 했습니까? – gaige

답변

2
 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
     { 
      [receivedData appendData:data]; 
     // declre receivedData as a property of the class(in xxx.m) 
      NSError *error=nil; 
      NSDictionary *result=[NSJSONSerialization JSONObjectWithData:data options: 
            NSJSONReadingMutableContainers error:&error]; 
      NSLog("%@", result); 
    //do whatever you want... 

      //the result dictionary is a JSON object and you can use it as KVC(key-value-coding) rules 
} 

앱이 iOS 5를 대상으로하는 경우 요청을 작성하는 방법으로 xxx.m 파일에이 코드를 사용하십시오. 그리고 다른 버전의 iOS에서 사용하고 싶다면 JSON 파서 프레임 워크를 살펴보십시오 ..

+0

당신은 그것에 대해 자세히 설명해 주시겠습니까? 왜냐하면 내가 사용하려고하기 때문에 많은 오류가 발생한다. – user1015777

+0

... .h 파일은 receivedData와 sysnthesyze의 속성을 선언한다. .. 그리고 receivedData는 nsmutabledata이며, nsmutabledictionary (또는 nes 사전). –

0

올바르게 연결을 설정하고있는 것 같습니다. 그러나 다음 NSURLConnection.h 파일에 기록 된있는 NSURLConnection 위임 방법의 데이터를 잡으려고 있습니다

특별한 관심의
@protocol NSURLConnectionDataDelegate <NSURLConnectionDelegate> 
@optional 
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response; 
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response; 

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

- (NSInputStream *)connection:(NSURLConnection *)connection needNewBodyStream:(NSURLRequest *)request; 
- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten 
               totalBytesWritten:(NSInteger)totalBytesWritten 
             totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite; 

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse; 

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

didReceiveDataconnectionDidFinishLoading 있습니다.