2016-08-09 9 views
0

私のアプリでは、サーバーにデータを投稿してレスポンスを受け取る必要があります。しかし私はdata.Belowを投稿した後にnull値を取得している私のフルコードです。前もって感謝します。オブジェクトを投稿してiosで返信する方法

{ 
     NSString *post =[[NSString alloc] initWithFormat:@"%@%@%@%@%@",[self.username_reg text],[self.emailid_reg text],[self.phone_reg text],[self.password_reg text],[self.confirmpassword_reg text]]; 
     NSLog(@"PostData: %@",post); 
     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
     NSURL *url=[NSURL URLWithString:@"https://servlet/URL"]; 

     NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

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


     [request setURL:url]; 
     [request setHTTPMethod:@"POST"]; 
     [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
     [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
     [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 


    NSMutableDictionary *postDict = [[NSMutableDictionary alloc] init]; 
    [postDict setValue:_username_reg.text forKey:@"UserName"]; 
    [postDict setValue:_emailid_reg.text forKey:@"Email"]; 
    [postDict setValue:_phone_reg.text forKey:@"Phone"]; 
    [postDict setValue:_password_reg.text forKey:@"Pass"]; 
    [postDict setValue:_confirmpassword_reg.text forKey:@"ConPass"]; 


    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:postDict options:0 error:nil]; 

    // Checking the format 
    NSString *urlString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 


    // Convert your data and set your request's HTTPBody property 
    NSString *stringData = [[NSString alloc] initWithFormat:@"jsonRequest=%@", urlString]; 
    NSData *requestBodyData = [stringData dataUsingEncoding:NSUTF8StringEncoding]; 

    request.HTTPBody = requestBodyData; 

     NSLog(@"bcbc:%@",requestBodyData); 
     [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) 
     { 
      NSString* newStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 
      NSLog(@"new:%@",newStr); 
      NSError *error; 
      NSDictionary *json_Dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 
        NSLog(@"%@",json_Dict); 
     }]; 

    } 

要求与えられたフォーマットは次のとおりです。

{"UserName":"sony","Email":"[email protected]","Phone":"7358700457","Pass":"sony88","ConPass":"sony88"} 

応答を取得する必要があります:

{"responseHeader":{"responseCode":0,"responseMessage":"Success"}} 
+0

どうAFNetworkingを使用してはどうですか?あなたの質問に対する答えではないかもしれません。しかし、それはネットワーキングの目的のために、非常に使いやすいです! –

+0

AFNetworkingの使い方を説明できます – Vignesh

+0

[AFNetworking](https://github.com/AFNetworking/AFNetworking):例を確認してください。 –

答えて

2

は、このコードを試してみてください。

NSData* jsonData = [NSJSONSerialization dataWithJSONObject:@"your dictionary name" options:NSJSONWritingPrettyPrinted error:&error]; 

NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 
NSLog(@"jsonString: %@", jsonString); 

NSData *requestData = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; 

NSMutableData *body = [NSMutableData data]; 
[body appendData:requestData]; 

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

NSURL *url = [NSURL URLWithString:@"your url"]; 
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url]; 
[request setHTTPMethod:@"POST"]; 
[request setHTTPShouldHandleCookies:NO]; 

[request setValue:@"application/json" forHTTPHeaderField:@"Content-type"]; 

[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setHTTPBody:body]; 

NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; 
[[session dataTaskWithRequest:request 
      completionHandler:^(NSData *data, 
           NSURLResponse *response, 
           NSError *error) 
    { 
     if (error) { 
      failure(error); 
     } else { 
      NSDictionary * jsonDic =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; 
     NSLog(@"%@",jsonDic); 
      if ([jsonDic objectForKey:@"error"]) { 

      } 
      else{ 

      } 

     } 
    }] resume]; 
関連する問題