Ios 使用AF3.0在URL正文中发送JSON数据

Ios 使用AF3.0在URL正文中发送JSON数据,ios,iphone,json,http,afnetworking-3,Ios,Iphone,Json,Http,Afnetworking 3,我想将URLbody中的JSON数据从我的IOS应用程序发送到服务器。我搜索了很多问题,但找不到我想要的。如果有人知道如何使用AFN网络,请告诉我。谢谢 这是我截取的发送参数的代码 NSMutableDictionary *parameters = [[NSMutableDictionary alloc]init]; [parameters setValue:@"PY" forKey:@"AppSecret"]; [parameters setValue:@"IOS" forKey:@"logi

我想将URLbody中的JSON数据从我的IOS应用程序发送到服务器。我搜索了很多问题,但找不到我想要的。如果有人知道如何使用AFN网络,请告诉我。谢谢 这是我截取的发送参数的代码

NSMutableDictionary *parameters = [[NSMutableDictionary alloc]init];
[parameters setValue:@"PY" forKey:@"AppSecret"];
[parameters setValue:@"IOS" forKey:@"login_provider"];
[parameters setValue:_txtemail.text forKey:@"email"];
[parameters setValue:_txtpassword.text forKey:@"password"];
[parameters setValue:@"1" forKey:@"ios_device_id"];


AFHTTPSessionManager *managertwo = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
managertwo.requestSerializer = [AFJSONRequestSerializer serializer];
[managertwo.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

[managertwo POST:[NSString stringWithFormat:@"http://capemedics.co.za/Api/user_register/valid_user"] parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    NSLog(@"success! %@",responseObject);
    NSLog(@"%@",parameters);

} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    NSLog(@"error: %@", error);
}];
我在这方面有错误。我想在URL正文中传递这样的JSON数据

{"login_provider": "IOS","email": "%@","password": ”%@“,”ios_device_id": "1"}

它以前工作过不确定

  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL
          cachePolicy:NSURLRequestReloadIgnoringCacheData  timeoutInterval:10];

 [request setHTTPMethod:@"POST"];

 [request setValue: @"application/json" forHTTPHeaderField:@"Content-Type"];
 [request setHTTPBody: [body dataUsingEncoding:NSUTF8StringEncoding]]; /// body is your son string

  AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
  op.responseSerializer = [AFJSONResponseSerializer serializer];
  [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

NSLog(@"JSON responseObject: %@ ",responseObject);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", [error localizedDescription]);

}];
[op start];

它以前工作过不确定

  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL
          cachePolicy:NSURLRequestReloadIgnoringCacheData  timeoutInterval:10];

 [request setHTTPMethod:@"POST"];

 [request setValue: @"application/json" forHTTPHeaderField:@"Content-Type"];
 [request setHTTPBody: [body dataUsingEncoding:NSUTF8StringEncoding]]; /// body is your son string

  AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
  op.responseSerializer = [AFJSONResponseSerializer serializer];
  [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

NSLog(@"JSON responseObject: %@ ",responseObject);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", [error localizedDescription]);

}];
[op start];
试试这个

NSString *strData = [[NSString alloc] initWithFormat:@"{\"login_provider\": \"IOS\",\"email\": \"%@\",\"password\": \"%@\",\"ios_device_id\": \"1\"}",self.txtEmail.text,self.txtPassword.text];

                NSString *strURL = @"http://capemedics.co.za/Api/user_register/valid_user"];

                NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:strURL] cachePolicy:NSURLRequestReloadIgnoringCacheData  timeoutInterval:10];

                [request setHTTPMethod:@"POST"];
                [request setValue: @"YOUR_KEY" forHTTPHeaderField: @"AppSecret"];
                [request setValue: @"application/json" forHTTPHeaderField:@"Content-Type"];
                [request setHTTPBody: [strData dataUsingEncoding:NSUTF8StringEncoding]];

                AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
                op.responseSerializer = [AFJSONResponseSerializer serializer];
                [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
                 {
                     if (responseObject)
                     {
                      }
                  }
                 } failure:^(AFHTTPRequestOperation *operation, NSError *error){
                 }];

                [op start];
试试这个

NSString *strData = [[NSString alloc] initWithFormat:@"{\"login_provider\": \"IOS\",\"email\": \"%@\",\"password\": \"%@\",\"ios_device_id\": \"1\"}",self.txtEmail.text,self.txtPassword.text];

                NSString *strURL = @"http://capemedics.co.za/Api/user_register/valid_user"];

                NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:strURL] cachePolicy:NSURLRequestReloadIgnoringCacheData  timeoutInterval:10];

                [request setHTTPMethod:@"POST"];
                [request setValue: @"YOUR_KEY" forHTTPHeaderField: @"AppSecret"];
                [request setValue: @"application/json" forHTTPHeaderField:@"Content-Type"];
                [request setHTTPBody: [strData dataUsingEncoding:NSUTF8StringEncoding]];

                AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
                op.responseSerializer = [AFJSONResponseSerializer serializer];
                [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
                 {
                     if (responseObject)
                     {
                      }
                  }
                 } failure:^(AFHTTPRequestOperation *operation, NSError *error){
                 }];

                [op start];

你试过什么?你能发布一些你尝试过的代码吗?错误是什么?你可以检查这个@reshmi。这个代码只发送参数,不发送JSONBody@viratpuar我建议您使用AFNetworking,但是您必须使用一个公共类来使用NSObject调用WS-url。这是非常简单和优化的代码。检查这里,但我也想传递URL正文。如何使用AFNetworking传递URL正文?您尝试了什么?你能发布一些你尝试过的代码吗?错误是什么?你可以检查这个@reshmi。这个代码只发送参数,不发送JSONBody@viratpuar我建议您使用AFNetworking,但是您必须使用一个公共类来使用NSObject调用WS-url。这是非常简单和优化的代码。检查这里,但我也想传递URL正文。如何使用AFNetworking传递URL正文?不需要此
NSMutableURLRequest
bro,一旦您的答案更新就好了不需要此
NSMutableURLRequest
bro,一旦您的答案更新就好了