0

我一直在尝试寻找有关如何对来自 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?或者我可以使用我提到的最后一个链接自己简单地做吗?我真的不知道从哪里开始。

谢谢你的时间。

4

1 回答 1

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;
}
于 2013-01-14T06:59:34.230 回答