u014297278
松鼠大作战
2016-05-31 08:35
采纳率: 66.7%
浏览 1.6k
已采纳

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条回答 默认 最新

  • oyljerry
    oyljerry 2016-05-31 09:20
    已采纳

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

    点赞 评论
  • u014297278
    松鼠大作战 2016-05-31 08:40

    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);
    }];
    

    简化

    点赞 评论
  • u014297278
    松鼠大作战 2016-06-01 02:34

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

    点赞 评论

相关推荐