1. Request failed: unauthorized (401)
+ (NSString *)dictionaryToJSONString:(NSDictionary *)dictionary
{
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
return jsonString;
}
+ (NSString *)arrayToJSONString:(NSArray *)array
{
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:array options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
// 字符串转Data
NSString *str =@"jesfds";
NSData *data =[str dataUsingEncoding:NSUTF8StringEncoding];
// NSData转NSString
NSString *result =[[ NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// data 转char
NSData *data;
char *test=[data bytes];
// char 转data
byte* tempData = malloc(sizeof(byte)*16);
NSData *content=[NSData dataWithBytes:tempData length:16];
2. 3840
错误信息如下:
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x9152780 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
错因:接口返回的是字符串,客户端却以JSON类型接收。
解决方案如下:
sessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
3. -1016
在进行网络请求时出现-1016 是因为只支持text/json,application/json,text/javascript。
解决方案如下:
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json",nil];
3. code 和 statusCode
- 获取statusCode
failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
NSInteger statusCode = [response statusCode];
}];
}
网友评论