2017-01-07 19 views
-1

我想这样的..如何发布{ “用户名”= “usernameValue”, “密码”= “passsworValue”}作为JSON体的目标C

-(void)GetCartIdDetails{ 

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
     NSString *post = [NSString stringWithFormat:@"username=%@&pasword=%@",self.TextUsername.text,self.TextPassword.text]; 
     NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
     NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]]; 
     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
     [request setURL:[NSURL URLWithString:@"http://192.168.0.21/mahroosa/rest/V1/integration/customer/token"]]; 

     [request setValue:@"application/json; charset=UTF-8" forHTTPHeaderField:@"Content-Type"]; 
     [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
     [request setHTTPMethod:@"POST"]; 
     [request setHTTPBody:postData]; 
     NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; 

     //MultiThreading 
     if (postData){ 
      dispatch_async(dispatch_get_main_queue(), ^{ 
       [[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 
        NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; 

        //removing Double Qoutes From String 
        NSString *Replace =[requestReply stringByReplacingOccurrencesOfString:@"\"" withString:@""]; 

        NSLog(@"requestReply: %@", Replace); 

       }] resume]; 
      }); 
     } 
    }); 
} 

使用AFNetworking:

-(void)Gettok { 

    NSString* URLString = [NSString stringWithFormat:@"http://192.168.0.21/mahroosa/rest/V1/integration/customer/token"]; 
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; 
    AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer]; 

    [requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
    [requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 


    manager.requestSerializer = requestSerializer; 

    NSMutableDictionary *params = [[NSMutableDictionary alloc] init]; 
    [params setObject:self.TextUsername.text forKey:@"username"]; 
    [params setObject:self.TextPassword.text forKey:@"password"]; 

     [manager POST:URLString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { 
      NSError * error; 
      NSArray *result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error]; 
      NSLog(@"--------------------respons : %@--------------------",result); 
     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { 
      NSLog(@"----------------------Error ; %@------------------------------",error); 
     }]; 
} 

请求主体的内容类型。此设定值“内容类型:应用程序/ JSON”

对此我得到解码错误信息。我已经得到了得到JSON的GetRequest在AFNetworking工作,但这个职位要求是给我一些问题。提前感谢您的帮助。

+1

你能显示你在控制台中得到的确切错误吗? –

回答

0

在第一个NSURLSession风格中,您不会将json发送到服务。试试看这个:

-(void)GetCartIdDetails{ 
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
      NSDictionary *dict = @{@"username":self.TextUsername.text, 
            @"password":self.TextPassword.text}; 


        NSData *postData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil]; 
         NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]]; 
         NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
         [request setURL:[NSURL URLWithString:@"http://192.168.0.21/mahroosa/rest/V1/integration/customer/token"]]; 

         [request setValue:@"application/json; charset=UTF-8" forHTTPHeaderField:@"Content-Type"]; 
         [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
         [request setHTTPMethod:@"POST"]; 
         [request setHTTPBody:postData]; 
         NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; 

         //MultiThreading 
         if (postData){ 
          dispatch_async(dispatch_get_main_queue(), ^{ 
           [[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 
            NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; 

            //removing Double Qoutes From String 
            NSString *Replace =[requestReply stringByReplacingOccurrencesOfString:@"\"" withString:@""]; 

            NSLog(@"requestReply: %@", Replace); 

           }] resume]; 
          }); 
         } 
      }); 
} 
+0

谢谢@ gulliva..It的作品。非常感谢。 – Shibili