在AFNetworking中传递NSNr的NSArray作为参数的正确方法

时间:2016-09-30 15:59:48

标签: ios nsarray afnetworking

我正在尝试发送POST请求AFNetworking这个包:

protected override IAsyncResult OnBeginGetToken(string appliesTo, string action,
    TimeSpan timeout, AsyncCallback callback, object state)
{
  var tcs = new TaskCompletionSource<SharedAccessSignatureToken>(state,
      TaskCreationOptions.RunContinuationsAsynchronously);
  var _ = CompleteAsync(GetCustomTokenAsync(appliesTo), callback, tcs);
  // _ is ignored; it can never fault.
  return tcs.Task;
}

private static async Task CompleteAsync<TResult>(Task<TResult> task,
    AsyncCallback callback, TaskCompletionSource<TResult> tcs)
{
  try
  {
    tcs.TrySetResult(await task.ConfigureAwait(false));
  }
  catch (OperationCanceledException ex)
  {
    tcs.TrySetCanceled(ex.CancellationToken);
  }
  catch (Exception ex)
  {
    tcs.TrySetException(ex);
  }
  finally
  {
    // Invoke callback unsafely on the thread pool, so exceptions are global
    if (callback != null)
      ThreadPool.QueueUserWorkItem(state => callback((IAsyncResult)state), tcs.Task);
  }
}

protected override SecurityToken OnEndGetToken(IAsyncResult result,
    out DateTime cacheUntil)
{
  var task = (Task<SharedAccessSignatureToken>)result;
  var ret = task.GetAwaiter().GetResult();
  cacheUntil = ...;
  return ret;
}

服务器返回错误,好像我在JSON中犯了错误

在这种情况下,服务器返回成功:

NSDictionary*parameters = [NSDictionary dictionaryWithObjectsAndKeys:[NSArray arrayWithObjects:@719,@714, nil],@"rules", nil];

[manager POST:path parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        triedAuthorize = NO;

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

    }];

这是成功的

NSDictionary*parameters = @{@"rules":@"[719,714]"}

2 个答案:

答案 0 :(得分:3)

NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:[NSArray arrayWithObjects:[NSNumber numberWithInt:719],[NSNumber numberWithInt:714],nil],@"rules", nil];

使用上述方法可解决您的问题

答案 1 :(得分:0)

我已经解决了这个问题

问题不在数组中作为参数。我添加了这一切,一切正常:

manager.requestSerializer = [ALFJSONRequestSerializer serializer];