【问题标题】:objective c calling wcf rest service request目标 c 调用 wcf 休息服务请求
【发布时间】:2013-01-14 06:48:48
【问题描述】:

我一直在尝试寻找有关如何对来自 wcf 服务的请求进行编码的在线文章/教程。我已将以下 Web 服务上传到我的服务器:

[ServiceContract]
    public interface IUserAccountService
    {
        [OperationContract]
        [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "UserLogIn?id={email}&password={password}")]
        AuthenticationToken UserLogIn(string email, string password);
    }

我对我一直在寻找的与之相关的文章或 SO 问题感到非常困惑:

例如:

  • -http://stackoverflow.com/questions/1557040/objective-c-best-way-to-access-rest-api-on-your-iphone

  • -http://stackoverflow.com/questions/8650296/nsjsonserialization-parsing-response-data

最后偶然发现了这个:

http://iam.fahrni.ws/2011/10/16/objective-c-rest-and-json/

所以我的问题是,我真的需要使用 restful 框架来调用 api 吗?如果是这样,哪个更推荐 - ASIHttpRequestRestKitAFNetworking?或者我可以使用我提到的最后一个链接自己简单地做吗?我真的不知道从哪里开始。

感谢您的宝贵时间。

【问题讨论】:

标签: objective-c wcf wcf-data-services nsjsonserialization


【解决方案1】:

NSURLConnection 和 NSJSONSerialization 工作正常。

编辑:我的一个项目中的一些示例代码,为简洁起见进行了编辑。
fstr(...) 只是 [NSString stringWithFormat:...]
的包装器 我使用 GCD 在后台线程上调用此代码。它不是线程安全的。

- (NSMutableURLRequest *)buildGetRequestHeaderWithMethod:(NSString *)method
{
  NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
    initWithURL:[NSURL URLWithString:fstr(@"%@%@", self.url, method)]];
  [request setTimeoutInterval:10.0];
  [request setHTTPMethod:@"GET"];
  [request setValue:self.key forHTTPHeaderField:@"Authentication"];
  [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
  return request;
}

- (id)callMethod:(NSString *)method
{
  NSMutableURLRequest *request = [self buildGetRequestHeaderWithMethod:method];
  return [self sendRequest:request withMethod:method];
}

- (id)sendRequest:(NSMutableURLRequest *)request withMethod:(NSString *)method
{
  NSHTTPURLResponse *response = nil;
  NSError *error = nil;
  [state() pushNetworkActivity];
  NSData *result = [NSURLConnection sendSynchronousRequest:request
    returningResponse:&response error:&error];
  [state() popNetworkActivity];
  self.lastStatusCode = response.statusCode;
  // Bug in Cocoa. 401 status results in 0 status and NSError code -1012.
  if(error && [error code] == NSURLErrorUserCancelledAuthentication)
  {
    [self interpretHTTPError:401 URLError:error forMethod:method];
    self.lastStatusCode = 401;
    return nil;
  }
  if(response.statusCode != 200)
  {
    [self interpretHTTPError:response.statusCode URLError:error forMethod:method];
    return nil;
  }
  id jsonResult = [self parseJsonResult:result];
  debug(@"%@", jsonResult);
  return jsonResult;
}


- (void)interpretHTTPError:(int)statusCode URLError:(NSError *)urlError
  forMethod:(NSString *)method
{
  NSString *message = fstr(@"HTTP status: %d", statusCode);
  if(statusCode == 0)
    message = [urlError localizedDescription];

#ifdef DEBUG
    message = fstr(@"%@ (%@)", message, method);
#endif

  if(self.alertUserOfErrors)
  {
    dispatch_async(dispatch_get_main_queue(), ^{
      errorMessage (message);
    });
  }
  else
    debug(@"%@", message);
  self.lastErrorMessage = message;
}

- (id)parseJsonResult:(NSData *)result
{
  if( ! result)
    return nil;
  NSError *error = nil;
  id jsonResponse = [NSJSONSerialization JSONObjectWithData:result
    options:NSJSONReadingMutableContainers error:&error];
  if(error)
  {
    NSLog(@"JSONObjectWithData failed with error: %@\n", error);
    return nil;
  }
  return jsonResponse;
}

【讨论】:

  • 你知道我可以查看的任何示例源代码,以便我了解序列是如何工作的吗?
  • 太棒了,一会儿试试看!谢谢你!我也可能有一些问题。
  • 嗨,所以在你的代码中,你是否调用了“callMethod”,它会返回一个解析的 json 吗?并作为字符串?我想知道是否可以使用它来检索模型详细信息?如果我可以返回一个 id 是否可以用来分配给该模型的新实例?例如。事件 {"Id":1,"EventName":"Test 1", "EventAddress":"address 1"}
  • 作为后续,这两行是做什么用的? [请求setValue:self.key forHTTPHeaderField:@"Authentication"]; [请求 setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
  • 它将解析后的 json 返回为 NSMutableDictionary/NSMutableArray 对象,叶子节点中有 NSString、NSNumber、NSNull 等。
猜你喜欢
  • 2011-08-10
  • 1970-01-01
  • 1970-01-01
  • 2021-10-20
  • 1970-01-01
  • 1970-01-01
  • 2012-02-29
  • 1970-01-01
  • 2012-06-28
相关资源
最近更新 更多