gpt4 book ai didi

ios - 使用 NSUrlSession 的服务器没有响应

转载 作者:行者123 更新时间:2023-11-29 01:13:35 25 4
gpt4 key购买 nike

嗨,我是 ios 的新手,在我的项目中,我正在使用 NSurlSEssion 将我的应用程序与服务集成,但是当我向服务器发送请求时,服务器没有响应,请帮助我:-

我的主要类(class):-

 NSString * finalString = @"UserName=101229518299&Password=123456&grant_type=Password";

[post5 postServieCalling:@"%@myUrl" :finalString];

服务器后台类:-

-(void)postServieCalling :(NSString*)mainurl :(NSString*)params{

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:mainurl,ServerBaseURL]]

cachePolicy:NSURLRequestUseProtocolCachePolicy

timeoutInterval:60.0];

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

NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

dispatch_async(dispatch_get_main_queue(), ^{

});

if (error) {

NSLog(@"dataTaskWithRequest error: %@", error);

NSString * BasicnetworkError = [error localizedDescription];
NSString * AppendString = @"Http Response failed with the following";
NSString * networkError = [AppendString stringByAppendingString:BasicnetworkError];

[self BasicError1:networkError];

}

else if ([response isKindOfClass:[NSHTTPURLResponse class]]) {

NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];

if (statusCode != 200) {

NSError *parseError;
id responseObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
[self MainService:responseObject];

}else{

NSError *parseError;

id responseObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];

NSLog(@"else condtion");

if (!responseObject) {

NSLog(@"JSON parse error: %@", parseError);

NSLog(@"responseobject is%@",responseObject);

} else {


NSLog(@"responseobject is %@",responseObject);

[self MainService:responseObject];
}

//if response was text/html, you might convert it to a string like so:

NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"final responseString = %@", responseString);
}
}
}];

[task resume];
}

最佳答案

我认为您没有在应用程序的 info.plist 中包含应用程序传输安全性请添加该 key ,您的代码将正常运行。

关于ios - 使用 NSUrlSession 的服务器没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35482787/

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