2016-08-09 9 views
0

こんにちは、私は私のアプリを私は価値のリストを表示したいと思うmethod.Inのiosに新しいです。 要求のフォーマットは:postメソッドでサーバーにデータを要求する方法は?

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

iが使用されるコードは次のとおり

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); 
     } 
    } 
    ]; 

応答がある:(ヌル)

上記のように同じ形式で値を要求する方法のおかげあらかじめ。それは空の応答を返す理由

+1

は、サーバーを確認して下さい。または、応答が何であるか、そしてそれが有効なJSONかどうかを確認します。 – Avi

+0

リクエストフォーマット{"customerId": "000536"、 "requestHeader":{"userId": "000536"}} 私は、customerIdとUserIdの両方が同じ値を持っていることがわかりました。 それで、サーバーの人に同じ要求を更新するように依頼する必要があります。 そして、あなたがuserIdを** Request Headerとして渡す必要があるかもしれないその他のもの** instaedofは辞書を要求するためにそれを追加します – Bhavik

+0

私は "{" responseHeader ":{" responseCode ":0、" responseMessage " "成功"}、 "apptModel":[{"顧客ID": "000536"、 "perfomerid": "000004"、 "名前": "Dr"、 "LastName": "abc"、 "Gender": "MALE" "、" Specialization ":" abc "、" Description ":" abc "}、" "...... @ Avi @Bhavik – 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の変更点は何ですか?私はまだ(ヌル)値を取得します。 – Vignesh

+0

デバッグし、生成されたURLがブラウザで動作していることを確認します – PinkeshGjr

+0

はいブラウザで動作しています--PinkeshGjr。上記のコメントと私が示した回答を見つけてください。 – Vignesh

関連する問題