问题
I've been trying to look for online articles / tutorials on how to go about coding a request from a wcf service. I have the following web service uploaded to my server:
[ServiceContract]
public interface IUserAccountService
{
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "UserLogIn?id={email}&password={password}")]
AuthenticationToken UserLogIn(string email, string password);
}
I'm getting really confused with the articles or SO questions that are related to it that I've been finding:
eg:
-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
and finally stumbled upon this:
http://iam.fahrni.ws/2011/10/16/objective-c-rest-and-json/
So my question is, do I really need to use a restful frameworks to do a call to an api? If so which one is more recommended - ASIHttpRequest or RestKit or AFNetworking? Or can I just simple do it myself using the last link I mentioned? I really am not sure where to start.
Thanks for your time.
回答1:
NSURLConnection and NSJSONSerialization work fine.
edit: Some example code from one of my projects, edited for brevity.
fstr(...) is just a wrapper around [NSString stringWithFormat:...]
I call this code on a background thread with GCD. It's not thread safe.
- (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;
}
来源:https://stackoverflow.com/questions/14314316/objective-c-calling-wcf-rest-service-request