gpt4 book ai didi

ios - ASIHTTPRequest 的包装器

转载 作者:行者123 更新时间:2023-11-28 22:58:35 25 4
gpt4 key购买 nike

我正在使用 ASIHTTPRequest 框架在我的 iOS 应用程序中进行网络调用。但是我不想在我的应用程序中的所有 Controller 中直接使用它。于是想到围绕ASIHTTPRequest写一个层。我的代码使用这一层来使用 ASIHTTPRequest。好处是将来我应该能够用其他框架替换这个框架,我的代码将保持不变,只是层会改变。我想知道这样做的策略应该是什么。我应该从 ASIHTTPRequest 类继承我的类,还是实现我自己的类。我应该考虑实现什么方法。

目前我是这样实现的。我的包装器是

MyRequestHandler.h : NSObject       
@property ASIHTTPRequest *asiHttpReq;

-(void) sendAsyncGetRequest
{
self.asiRequest = [ASIHTTPRequest requestWithURL:self.url];
if(self.tag != 0){
self.asiRequest.tag = self.tag;
}
[self.asiRequest setDelegate:self];
[self.asiRequest startAsynchronous];
}

- (void)requestFinished:(ASIHTTPRequest *)request{
MyResponseObj *respone = <From request obj>
if([delegate respondsToSelector:@selector(requestFinished:)]){
[delegate performSelector:@selector(requestFinished:) withObject:response];
}
}

在我的 View Controller 中我会这样做:

MyViewController.h : UIViewContoller
@property MyRequestHandler *reqHandler;

-(void) fireRequest
{
NSString* requestUrl = <create URL>;
if(requestUrl){

// [self showLoadingIndicatorView];

// Proceed for request.
NSURL *url = [NSURL URLWithString:requestUrl];
reqHandler = [MyRequestHandler requestWithURL:url];
reqHandler.tag = 1000;
[reqHandler setDelegate:self];
[reqHandler sendAsyncGetRequest];
}
}

- (void)requestFinished:(MyResponse*) responseData{
// Do Your parsing n all here.
}

- (void)requestFailed:(MyResponse*) responseData{
// Handle the error here.
}

这是正确的做法吗?这里的问题是因为我在 viewcontroller 中创建了 myrequesthandler 的属性,我一次只能发出一个请求,并且失去了 ASIHTTPRequest 同时发出多个请求的能力。

你能建议我如何处理这样的问题吗?

最佳答案

这是我正在使用的:

#import "ASIFormDataRequest.h"

@interface RequestPerformer : NSObject {
id localCopy; // to avoid exec_bad_access with arc
ASIHTTPRequest *getRequest;
ASIFormDataRequest *postRequest;
}

@property (nonatomic, retain) id delegate;
@property (nonatomic, readwrite) SEL callback;
@property (nonatomic, readwrite) SEL errorCallback;

- (void)performGetRequestWithString:(NSString *)string stringDictionary:(NSDictionary *)stringDictionary delegate:(id)requestDelegate requestSelector:(SEL)requestSelector errorSelector:(SEL)errorSelector;
- (void)performPostRequestWithString:(NSString *)string stringDictionary:(NSDictionary *)stringDictionary dataDictionary:(NSDictionary *)dataDictionary delegate:(id)requestDelegate requestSelector:(SEL)requestSelector errorSelector:(SEL)errorSelector;

@end

//

#import "RequestPerformer.h"
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"

@implementation RequestPerformer

@synthesize delegate;
@synthesize callback, errorCallback;

- (void)performGetRequestWithString:(NSString *)string stringDictionary:(NSDictionary *)stringDictionary delegate:(id)requestDelegate requestSelector:(SEL)requestSelector errorSelector:(SEL)errorSelector {

localCopy = self;

self.delegate = requestDelegate;
self.callback = requestSelector;
self.errorCallback = errorSelector;

NSMutableString *requestStringData = [[NSMutableString alloc] init];
if (stringDictionary)
for (NSString *key in [stringDictionary allKeys])
[requestStringData appendFormat:@"%@=%@&", key, [stringDictionary objectForKey:key]];
NSString *resultString = [requestStringData substringToIndex:[requestStringData length]-1];

getRequest = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@", string, [resultString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]];
[getRequest setDelegate:self];
[getRequest setRequestMethod:@"GET"];

//NSLog(@"request url = %@", [getRequest.url absoluteString]);
[getRequest startAsynchronous];
}

- (void)performPostRequestWithString:(NSString *)string stringDictionary:(NSDictionary *)stringDictionary dataDictionary:(NSDictionary *)dataDictionary delegate:(id)requestDelegate requestSelector:(SEL)requestSelector errorSelector:(SEL)errorSelector {

localCopy = self;

self.delegate = requestDelegate;
self.callback = requestSelector;
self.errorCallback = errorSelector;

NSURL *url = [NSURL URLWithString:string];

postRequest = [[ASIFormDataRequest alloc] initWithURL:url];
[postRequest setDelegate:self];
[postRequest setRequestMethod:@"POST"];

if (stringDictionary)
for (NSString *key in [stringDictionary allKeys])
[postRequest setPostValue:[stringDictionary objectForKey:key] forKey:key];

if (dataDictionary)
for (NSString *key in [dataDictionary allKeys])
[postRequest setData:[dataDictionary objectForKey:key] forKey:key];

//NSLog(@"request url = %@", [postRequest.url absoluteString]);
[postRequest startAsynchronous];
}

#pragma mark - ASIHTTPRequest Delegate Implementation

- (void)requestFinished:(ASIHTTPRequest *)crequest {
NSString *status = [crequest responseString];

if (self.delegate && self.callback) {
if([self.delegate respondsToSelector:self.callback])
[self.delegate performSelectorOnMainThread:self.callback withObject:status waitUntilDone:YES];
else
NSLog(@"No response from delegate");
}
localCopy = nil;
}
- (void)requestFailed:(ASIHTTPRequest *)crequest {
if (self.delegate && self.errorCallback) {
if([self.delegate respondsToSelector:self.errorCallback])
[self.delegate performSelectorOnMainThread:self.errorCallback withObject:crequest.error waitUntilDone:YES];
else
NSLog(@"No response from delegate");
}
localCopy = nil;
}

@end

要使用它,只需在您的 UIViewController 中导入 RequestPerformer.h 并执行以下操作:

[requestManager performGetRequestWithString:tempString stringDictionary:stringDictionary dataDictionary:dataDictionary delegate:self requestSelector:@selector(requestSucceeded:) errorSelector:@selector(requestFailed:)];

参数:

  • (NSString *)string - url 字符串,在哪里发布你的请求;
  • (NSDictionary *)stringDictionary - 字典,其中包含所有文本信息(如姓名、身份证等);
  • (NSDictionary *)dataDictionary - 字典,包含所有数据信息(如照片、文件等);
  • (id)requestDelegate - 委托(delegate)执行下面的选择器;
  • (SEL)requestSelector - 选择器,请求成功后执行;
  • (SEL)errorSelector - 选择器,将在发生错误时执行。

关于ios - ASIHTTPRequest 的包装器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10361419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com