【发布时间】:2014-04-30 01:28:32
【问题描述】:
我有一个 PHP RESTful 服务器,我连接到它以根据我的 Objective-c 程序调用的 URL 路径调用方法。我使用ASIHTTPRequest 和SBJson。
一切正常,但我在不同的控制器中执行请求,现在所有这些控制器中都有重复的方法。在单个类中抽象该代码(请求)的最佳方法是什么,以便我可以轻松地重用代码,从而使控制器更简单。
请把我从我认为是冗余和重复的编码序列中解救出来。
我尝试过制作一个单例类,但是每个视图控制器需要不同的请求连接点,并且大多数视图控制器需要在调用成功的方法时调用特定的操作,我不知道如何处理这一切。
这是我的代码模板目前在 Objective-c 程序中的样子:
//*1) Somewhere at the top of my .m file I have this*
//These are the suffixes to the URL path that I connect to at my server,
//depending on the action required
NSString *const RequestCreateCustomer = @"Create/Customer";
NSString *const RequestUpdateCustomer = @"Update/Customer";
NSString *const RequestDeleteCustomer = @"Delete/Customer";
//*2) Then I have my connection invocation code*
//Method invocation, all of them look something like this
-navigationButton{
...
[self retrieveWithRequestStringType:RequestUpdateCustomer];
}
-(void)retrieveWithRequestStringType:(NSString*)typeOfRequest{
NSLog(@"Retrieve %@ method called", typeOfRequest);
NSString *urlString = [NSString stringWithFormat:@"%@/Secure/CB/%@", @"http://www.defaultStapleURLToMyServer.com/CB", typeOfRequest];
NSString *encodedUrlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [[NSURL alloc] initWithString:encodedUrlString];
serverRequest = nil;
serverRequest = [ASIFormDataRequest requestWithURL:url];
[serverRequest addRequestHeader:@"Content-Type" value:@"application/json"];
[serverRequest addRequestHeader:@"Request-Method" value:@"POST"];
//Normally at this point depending on the request type, I prepare some data that needs to be sent along with the request
NSMutableDictionary *completeDataArray = [[NSMutableDictionary alloc] init];
if([typeOfRequest isEqualToString:RequestCreateCustomer]){
[serverRequest setUserInfo:[NSDictionary dictionaryWithObject:RequestCreateCustomer forKey:@"RequestType"]];
if( ! [self validateAndPrepareAllData:&completeDataArray]){
return;
}
}
else if([typeOfRequest isEqualToString:RequestUpdateCustomer]){
[serverRequest setUserInfo:[NSDictionary dictionaryWithObject:RequestUpdateCustomer forKey:@"RequestType"]];
if( ! [self validateAndPrepareAllData:&completeDataArray]){
return;
}
}
else if([typeOfRequest isEqualToString:RequestDeleteCustomer]){
[serverRequest setUserInfo:[NSDictionary dictionaryWithObject:RequestDeleteCustomer forKey:@"RequestType"]];
if( ! [self validateAndPrepareCustomerIdData:&completeDataArray]){
return;
}
}
NSString *jsonString = [completeDataArray JSONRepresentation];
[serverRequest appendPostData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
[serverRequest setDelegate:self];
[serverRequest setDidFinishSelector:@selector(requestSucceeded:)];
[serverRequest setDidFailSelector:@selector(requestFailed:)];
[serverRequest startAsynchronous];
}
//*3) And heres the connection did succeed, and connection did fail methods*
-(void)requestSucceeded:(ASIHTTPRequest*)request{
NSInteger statusCode = [[[request responseHeaders] objectForKey:@"StatusCode"] intValue];
NSLog(@"StatusCode: %@", [[request responseHeaders] objectForKey:@"StatusCode"]);
NSString *myString = [[NSString alloc] initWithData:[request responseData] encoding:NSUTF8StringEncoding];
NSDictionary *JSONDictionary = [myString JSONValue];
switch (statusCode) {
case 400:
case 401:
{
NSLog(@"display error message");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[[request.responseString JSONValue] objectForKey:@"Message"] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
break;
}
case 200:
NSLog(@"status code = 200 so successful");
//Created customer request succeeded
if([[[request userInfo] objectForKey:@"RequestType"] isEqualToString:RequestCreateCustomer]){
[self.delegate addedCustomerVCWithCustomer:[[CustomerDataModel alloc] initWithJSONData:[JSONDictionary objectForKey:@"CustomerObject"]]];
[self cancelModalView];
}
//Edit customer request succeeded
else if([[[request userInfo] objectForKey:@"RequestType"] isEqualToString:RequestUpdateCustomer]){
[self.delegate editedCustomerVCWithCustomer:[[CustomerDataModel alloc] initWithJSONData:[JSONDictionary objectForKey:@"CustomerObject"]]];
[self cancelModalView];
}
//Delete customer request succeeded
else if([[[request userInfo] objectForKey:@"RequestType"] isEqualToString:RequestDeleteCustomer]){
[self.delegate deletedCustomer:customer];
[self cancelModalView];
}
break;
default:
[SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"went to none: %d or %@", (int)statusCode, [[request responseHeaders] objectForKey:@"StatusCode"]]];
NSLog(@"went to none: %d or %@", (int)statusCode, [[request responseHeaders] objectForKey:@"StatusCode"]);
break;
}
[SVProgressHUD dismiss];
}
-(void)requestFailed:(ASIHTTPRequest*)request{
if([[[request userInfo] objectForKey:@"RquestType"] isEqualToString:RequestCreateCustomer]){
NSLog(@"retrieving states failed so trying again");
[self addCustomerRequest];
}
else if(){
//... you get the point
}
}
有人可以提供替代解决方案吗?
【问题讨论】:
-
我尝试了一个简单的方法。看看我的问题stackoverflow.com/questions/19706004/…
-
嘿,谢谢,伙计,你使用的块很好,但它仍然意味着跨多个视图控制器的重复,我真的不想要这种规模。
标签: ios objective-c json asihttprequest