gpt4 book ai didi

ios - NSUrlSession 不工作

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

嗨,我是 ios 的新手,在我的项目中,我正在使用 NSUrlSession 来调用服务

但在我下面的代码中,我维护了处理服务器响应的 if 和 else 条件,但那些 if 和 else 条件不调用

请帮我看看错在哪里?

- (void)viewDidLoad {
[super viewDidLoad];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:myurl here]];

cachePolicy:NSURLRequestUseProtocolCachePolicy

timeoutInterval:60.0];


[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"GET"];
[request setHTTPBody:[self httpBodyForParamsDictionary:params]];


//You now can initiate the request with NSURLSession or NSURLConnection, however you prefer. For example, with NSURLSession, you might do:

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


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) {

NSLog(@"Expected responseCode == 200; received %ld", (long)statusCode);

NSString *statusCodeError = [NSString stringWithFormat: @"Http Response failed with the following code %ld", (long)statusCode];

[self BasicError1:statusCodeError];
}
}

// If response was JSON (hopefully you designed web service that returns JSON!),
// you might parse it like so:

else{

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

NSLog(@"else condtion");

if (!responseObject) {

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

} else {

NSLog(@"responseObject = %@", 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];
}

- (NSData *)httpBodyForParamsDictionary:(NSDictionary *)paramDictionary{

NSMutableArray *parameterArray = [NSMutableArray array];

[paramDictionary enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) {
NSString *param = [NSString stringWithFormat:@"%@=%@", key, [self percentEscapeString:obj]];
[parameterArray addObject:param];
}];

NSString *string = [parameterArray componentsJoinedByString:@"&"];

return [string dataUsingEncoding:NSUTF8StringEncoding];
}

- (NSString *)percentEscapeString:(NSString *)string{

NSString *result = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)string,
(CFStringRef)@" ",
(CFStringRef)@":/?@!$&'()*+,;=",
kCFStringEncodingUTF8));
return [result stringByReplacingOccurrencesOfString:@" " withString:@"+"];
}

最佳答案

您的代码中提到的 if else block 有一个错误的情况。请使用下面的代码。

- (void)viewDidLoad {
[super viewDidLoad];

NSDictionary *mainDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
@"COLLECTION",@"SearchBy",
@"1284",@"SearchKey",
@"",@"Color",
@"",@"PriceFrom",
@"",@"PriceTo",
@"",@"QtyFrom",
@"",@"QtyTo",
nil];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://203.77.214.78/StockManager/SL/SearchProducts"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];

[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[self httpBodyForParamsDictionary:mainDictionary]];


//You now can initiate the request with NSURLSession or NSURLConnection, however you prefer. For example, with NSURLSession, you might do:

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


if (error) {

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

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

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

if (statusCode != 200) {

NSLog(@"Expected responseCode == 200; received %ld", (long)statusCode);

}else{

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

NSLog(@"else condtion");

if (!responseObject) {

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

} else {

NSLog(@"responseObject = %@", 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];
}

关于ios - NSUrlSession 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35192986/

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