2012-07-16 2 views
1

나는 그것이 비동기 요청을 보내려고하는 작업을해야하지만, 요청이 생성되고 있지 않습니다과 같은 몇 가지 코드가있다 :있는 NSURLConnection sendAsynchronousRequest : 시작하지 요청

-(void) setEmail: (NSString *) subject andBody: (NSString *) body 
{ 
    NSString *urlString = @"http://www.my_url.com?";  

    // Create encoded query string 
    NSString *query_string = @"subject=%@&body=%@"; 
    NSString *query_with_args = [NSString stringWithFormat:query_string , subject , body];  

    // Now encode the query 
// NSString *encodedQuery = (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, 
//                       (__bridge CFStringRef)query_with_args, 
//                       NULL, 
//                       (CFStringRef)@"!*'();:@&=+$,/?%#[]", 
//                       kCFStringEncodingUTF8); 
// 

    // Now concatinate url and query  
    NSString *final_url = [urlString stringByAppendingString:query_with_args]; 

    NSURL *url = [NSURL URLWithString:final_url]; 

    // Now send to the server  
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url]; 

    // TODO: ok I dont really understand what this is 
    NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 

    NSLog(@"......before request"); 

    [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) 
    {   
     NSLog(@"On return"); 

     NSLog(@"This is data: %@" , data); 
     NSLog(@"This is response: %@" , response); 
     NSLog(@"This is error: %@" , error); 

     NSLog(@"OK");   
    }];   
} 
: 여기

는 기능입니다

무엇이 잘못되었을 수 있으며 요청이 전송되지 않은 이유는 무엇입니까?

와의 로거

NSLog (@ "...... 요청 전"); 인쇄되지 않습니다.

+0

입니까? – mkral

+0

@mkral 예, 작동 중입니다. 그러나 응답하지 않더라도, 나는 여전히 나쁜 요청이나 어떤 종류의 것을 얻는다. 아니? – GeekedOut

+0

참으로 무한 루프가 아니라면 나쁜 요청 인 경우에도 응답을받을 수 있습니다. AFNetworking을 조사해 보셨습니까? 사용하기 쉽고 정말 유용합니다. – mkral

답변

1

다음과 같이 시도하십시오. NSURLConnectionDelegate 프로토콜과 메소드를 구현해야합니다. 당신은 컬이나 뭔가를 테스트 할 때 응답하는 웹 서비스는

NSString *final_url = [NSString stringWithFormat:@"http://www.my_url.com?subject=%@&body=%@",subject, body]; 
NSURL *url = [NSURL URLWithString:final_url]; 
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url]; 
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 
    if(theConnection) 
    { 
     webData = [NSMutableData data]; 
    } 
    else 
    { 
     NSLog(@"theConnection is NULL"); 
    } 
} 

희망이 하나가 당신을 도울 것입니다 ... http://www.cocoaintheshell.com/2011/04/nsurlconnection-synchronous-asynchronous/

+0

코드가 작동했지만 시도한 때 : [NSURLConnection sendAsynchronousRequest : theRequest queue : queue completionHandler :^(NSURLResponse * response, NSData * data, NSError * error) 여전히 작동하지 않았다. ( – GeekedOut

+0

당신은 queue.But에서 NSWperationQueue.So로 작업하는 방법을 모른다. 그 코드를 –

+0

으로 만들면 큐를 주석으로 처리해야합니까? – GeekedOut

관련 문제