2016-08-09 2 views
0

안녕하세요, 저는 iOS 게시 메서드에 새로운입니다. 내 응용 프로그램에서 값 목록을 표시하려고합니다. 요구 포맷은 :post 메소드에서 서버에 데이터를 요청하는 방법은 무엇입니까?

{"customerId":"000536","requestHeader":{"userId":"000536"}} 

제가 사용되는 코드이다

NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 
    NSString *post =[[NSString alloc] initWithFormat:@"customerId=%@&userId=%@",@"000536",@"000536"]; 
    NSLog(@"PostData: %@",post); 

    NSURL *url=[NSURL URLWithString:@"https://servelet/URL"]; 
    NSDictionary *jsonDict = [[NSDictionary alloc] initWithObjectsAndKeys: 
           @"000536", @"customerId", 
           @"000536", @"userId", 
           nil]; 

    NSError *error; 
    NSData *postData = [NSJSONSerialization dataWithJSONObject:jsonDict options:0 error:&error]; 

    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]]; 

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
    [request setURL:url]; 
    [request setHTTPMethod:@"POST"]; 
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
    [request setValue:@"application/json; character=utf-8" forHTTPHeaderField:@"Content-Type"]; 
    [request setHTTPBody:postData]; 

    //[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]]; 
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response,NSData *data, NSError *error){ 
     // NSLog(@"Response code: %ld", (long)[response statusCode]); 
     if(error || !data){ 
      NSLog(@"Server Error : %@", error); 
     } 
     else 
     { 
      NSLog(@"Server Response :%@",response); 
      NSError* error; 
      NSDictionary* json = [NSJSONSerialization 
            JSONObjectWithData:data 
            options:kNilOptions 
            error:&error]; 

      NSArray* latest = [json objectForKey:@"apptModel"]; 
      NSLog(@"items: %@", latest); 
     } 
    } 
    ]; 

반응이다 : (NULL)

전술 한 바와 같이 동일 형식의 값을 요청하는 방법

감사 미리. 이 빈 응답을 반환하는 이유

+1

는 서버에게 문의하십시오. 또는 응답이 무엇인지, 그리고 그것이 유효한 JSON인지 확인하십시오. – Avi

+0

요청 형식 { "customerId": "000536", "requestHeader": { "userId": "000536"}} customerId와 UserId가 둘 다 동일한 값을 갖는 것을 발견했습니다. 그래서 서버 사용자에게 동일한 요청을 업데이트하도록 요청해야합니다. 그리고 당신은 ** 요청 헤더 ** instaedof 요청 사전에 – Bhavik

+0

응답을 추가 할 userId를 전달해야 할 수도 있습니다 나는 "{"responseHeader ": {"responseCode ": 0,"responseMessage ": "성과", "apptModel": [{ "customerid": "000536", "perfomerid": "000004", "이름": "박사", "성": "abc", "성별": "남성" ...... "Avi"@Bavik – Vignesh

답변

1

사용이 코드

NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 
NSString *post =[[NSString alloc] initWithFormat:@"customerId=%@&userId=%@",@"000536",@"000536"]; 
NSLog(@"PostData: %@",post); 

NSURL *url=[NSURL URLWithString:@"https://servelet/URL"]; 
NSDictionary *jsonDict = [[NSDictionary alloc] initWithObjectsAndKeys: 
          @"000536", @"customerId", 
          @"000536", @"userId", 
          nil]; 

NSError *error; 
NSData *postData = [NSJSONSerialization dataWithJSONObject:jsonDict options:0 error:&error]; 

NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]]; 

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
[request setURL:url]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
[request setValue:@"application/json; character=utf-8" forHTTPHeaderField:@"Content-Type"]; 
[request setHTTPBody:postData]; 

//[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]]; 
NSURLSession *session = [NSURLSession sharedSession]; 
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *err) 
{ 
    // NSLog(@"Response code: %ld", (long)[response statusCode]); 
    if(error || !data){ 
     NSLog(@"Server Error : %@", error); 
    } 
    else 
    { 
     NSLog(@"Server Response :%@",response); 
     NSError* error; 
     NSDictionary* json = [NSJSONSerialization 
           JSONObjectWithData:data 
           options:kNilOptions 
           error:&error]; 

     NSArray* latest = [json objectForKey:@"apptModel"]; 
     NSLog(@"items: %@", latest); 
    } 
}]; 
[task resume]; 
+0

@PinkeshGjr의 변경 내용은 무엇입니까? 나는 여전히 (null) 값을 얻는다. – Vignesh

+0

디버그하고 생성 된 URL이 브라우저에서 작동하는지 확인하십시오. – PinkeshGjr

+0

예, 브라우저에서 작동합니다. --PinkeshGjr. 위의 의견과 응답을 찾으십시오. – Vignesh

관련 문제