【问题标题】:Post an array of geoPoint as parameters, but the data from api response is nil?发布 geoPoint 数组作为参数,但来自 api 响应的数据为零?
【发布时间】:2017-11-23 19:19:47
【问题描述】:

有一个api,我需要发布geoPoint,然后获取数据,这在Postman 上运行良好。如图所示, this api works fine on Postman

在我的代码中,我使用如下方式调用api

NSArray *geoPoint = @[@114.33f, @22.44f];

NSDictionary *geoPointDic = @ {@"geoPoint" : geoPoint};
NSDictionary *inData = @{
                         @"action" : @"getNearbyEventList",
                         @"data" : geoPointDic};

NSDictionary *parameters = @{@"data" : inData};

NSLog(@"geoPoint is %@", geoPoint);
NSLog(@"upcoming events parameters %@", parameters);

[_manager POST:GetURL parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  responseObject) {

    NSLog(@"responseObject is %@", responseObject);
    NSLog(@"responseObject - data is %@", responseObject[@"data"]);

    NSArray *eventsArray = responseObject[@"data"];

但是输出是

NSLog参数:

events parameters {
    data =     {
        action = getNearbyEventList;
        data =         {
            geoPoint =             (
                "114.33",
                "22.44"
            );
        };
    };
}

the api response looks like

我猜我的参数可能格式错误,所以我尝试了以下方式,但没有人可以工作

//NSArray *geoPoint = [[NSArray alloc] init];

//NSArray *geoPoint = [[NSArray alloc] initWithObjects:@"114", @"22", nil];

//NSMutableArray *geoPoint = [[NSMutableArray alloc] init];

//[geoPoint addObject:@114];

//[geoPoint addObject:@22];

//NSArray *geoPoint = [[NSArray alloc] initWithObjects:@"114", @"22", nil];

//NSString *geoPoint = @"[114.33,22.44]";

那么,谁能告诉我如何获取Postman 中显示的数据?

非常感谢!

【问题讨论】:

    标签: ios objective-c json api afnetworking


    【解决方案1】:

    你可以试试:

     NSData *dataBody = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:nil];
     NSString *json = [[NSString alloc] initWithData:dataBody encoding:NSUTF8StringEncoding];
     NSLog(@"%@",json);
    

    对象 json 与您发送到服务器的数据相同。

    【讨论】:

    • 非常感谢您的回答。我努力了。但是如果我不发送 NSDictionary 而是使用 NSString 作为参数,就会出现“请求失败:内部服务器错误(500)”。 >
    • 我认为它对你有用。 stackoverflow.com/questions/19099448/…
    猜你喜欢
    • 1970-01-01
    • 2015-04-01
    • 1970-01-01
    • 2020-06-30
    • 2019-05-15
    • 2015-12-30
    • 1970-01-01
    • 2018-12-26
    • 1970-01-01
    相关资源
    最近更新 更多