ios http网络请求,返回 null

图片说明
代码如图,该接口在浏览器中能正常访问,但在xcode里显示如图。xcode里其他接口正常
下面是代码

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
manager.requestSerializer.timeoutInterval = 20.f;
[manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/plain", @"text/html", nil];
NSString *urlStr = [URLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@" %@",urlStr);
[manager POST:urlStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
// NSLog(@"suc: %@", string);
NSDictionary *dicRoot = [[[SBJsonParser alloc]init] objectWithString:string];
NSString *desc = [dicRoot objectForKey:@"desc"];

    NSDictionary *dicRoot2 = [[[SBJsonParser alloc]init] objectWithString:desc];
  //  verify = [dicRoot2 objectForKey:@"verify"];
    //verify是获取到的被加密的验证码

    NSLog(@"suc--: %@", desc);


} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

3个回答

你这个已经走的返回成功了,先看看你的服务器这个接口是否返回了数据。

u014297278
松鼠大作战 返回了,浏览器中能显示出来数据,但是在app里就是null
3 年多之前 回复

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
manager.requestSerializer.timeoutInterval = 20.f;
[manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/plain", @"text/html", nil];

[manager POST:urlStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
   //  NSLog(@"suc: %@", string);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

简化

解决了,原因是传回的responseObject 编码问题导致null,NSStringEncoding enc= CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
NSString *string = [[NSString alloc]initWithData:responseObject encoding:enc];
解决

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问