gpt4 book ai didi

ios - 通过使用 JSON 的 POST 从 NSURLSession 取回数据

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:23:50 25 4
gpt4 key购买 nike

由于 NSURLConnection 已弃用,我需要转移到 NSURLSession。我有一个 URL 和一些需要作为 JSON 输入的数据。那么返回的结果应该是JSON。我看到这样的东西:

NSError *error;

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSURL *url = [NSURL URLWithString:@"[JSON SERVER"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];

[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];

[request setHTTPMethod:@"POST"];
NSDictionary *mapData = [[NSDictionary alloc] initWithObjectsAndKeys: @"TEST IOS", @"name",
@"IOS TYPE", @"typemap",
nil];
NSData *postData = [NSJSONSerialization dataWithJSONObject:mapData options:0 error:&error];
[request setHTTPBody:postData];


NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

}];

[postDataTask resume];

这是正确的方法吗?

我的要求是:1. 将我的键值对转换为 JSON。2. 将 URL 和 JSON 传递给可重用函数。3.获取返回的JSON数据。4.解析返回的JSON数据。

最佳答案

让您的方法的调用者提供一个完成处理程序来处理返回的数据并更新 UI 以指示完成。

您可以复制在 SDK 中找到的模式,如下所示:

- (void)makeRequest:(NSString *)param completion:(void (^)(NSDictionary *, NSError *))completion;

这样实现:

// in the same scope
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];

- (void)makeRequest:(NSString *)param
completion:(void (^)(NSDictionary *, NSError *))completion {

// your OP code goes here, e.g.
NSError *error;
NSMutableURLRequest *request = // maybe the param is the url for this request
// use the already initialized session
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

// call the completion handler in EVERY code path, so the caller is never left waiting
if (!error) {
// convert the NSData response to a dictionary
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (error) {
// there was a parse error...maybe log it here, too
completion(nil, error);
} else {
// success!
completion(dictionary, nil);
}
} else {
// error from the session...maybe log it here, too
completion(nil, error);
}
}];
[postDataTask resume];
}

调用此方法的代码如下所示:

// update the UI here to say "I'm busy making a request"
// call your function, which you've given a completion handler
[self makeRequest:@"https://..." completion:^(NSDictionary *someResult, NSError *error) {
// here, update the UI to say "Not busy anymore"
if (!error) {
// update the model, which should cause views that depend on the model to update
// e.g. [self.someUITableView reloadData];
} else {
// handle the error
}
}];

注意几件事:(1) 返回类型为 void,调用者不希望从该方法返回任何内容,并且在调用它时不进行赋值。 “返回”的数据作为参数提供给完成处理程序,稍后在 asnych 请求完成后调用它,(2) 完成处理程序的签名与调用方在完成 block 中声明的完全匹配 ^( NSDictionary *, NSError *),这只是一个建议,典型的网络请求。

关于ios - 通过使用 JSON 的 POST 从 NSURLSession 取回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37624189/

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