gpt4 book ai didi

ios - 使用 AFNetworking 3.0 在 URL 正文中发送 JSON 数据

转载 作者:行者123 更新时间:2023-11-28 21:32:10 25 4
gpt4 key购买 nike

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

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 Body 中传递这样的 JSON 数据

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

最佳答案

试试这个

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

关于ios - 使用 AFNetworking 3.0 在 URL 正文中发送 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35478933/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com