2017-01-03 5 views
0

내가 인쇄 한 데모 코드 웹 서비스에서 데이터 만든,NSURLSession을 사용하여 웹 서비스에서 데이터를 가져 오는 방법은 무엇입니까?

코드

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 

    [request setURL:[NSURL URLWithString:@"http://54.149.46.64/API/video.php"]]; 
    [request setHTTPMethod:@"POST"]; 
    [request setValue:@"video" forHTTPHeaderField:@"list"]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 


    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; 
    [[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 
     NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; 
     NSLog(@"requestReply: %@", requestReply); 
    }] resume]; 

출력 내가 WS에서 데이터를 가져올 수 없습니다 나는 왜

JSON_Webservices[4496:150468] requestReply: {"Status":"Fail","Message":"Please enter required fields"} 

? 도와주세요.

작업 벌금 이죠

이미지

enter image description here

감사합니다.

enter image description here

헤더는

enter image description here

+1

난 당신이 비디오의 목록을 forHTTPHeaderField를 설정하지한다고 생각합니다. HTTPBody로 설정하십시오 –

+0

어떻게 설정합니까? @Dheeraj – user7018875

+0

POSTMan에서 전달하는 매개 변수는 무엇입니까? –

답변

3

이를 확인하시기 바랍니다 ...

NSURL *aUrl = [NSURL URLWithString:@"XXXX"]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl 
                  cachePolicy:NSURLRequestUseProtocolCachePolicy 
                 timeoutInterval:60.0]; 

    [request setHTTPMethod:@"POST"]; 
    NSString *postString = @"list=video"; 
    [request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]]; 

    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; 
    [[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 
     NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; 
     NSLog(@"requestReply: %@", requestReply); 

     NSError *jsonError; 
     NSData *objectData = [requestReply dataUsingEncoding:NSUTF8StringEncoding]; 
     NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData 
                  options:NSJSONReadingMutableContainers 
                   error:&jsonError]; 
     NSLog(@"requestReply: %@", json); 

    }] resume]; 
+0

@user7018875이 코드를 복사하여 지난 후에 확인하십시오. –

+0

고마워요 buddyyyyy ..... 고마워요 – user7018875

+0

Working Fine .... – user7018875

관련 문제