我正在开发客户端 - 服务器应用。我发现很少有教程如何上传一个图像。但是使用单个请求上传多个图像的最佳方法是什么?我是网络编程的新手,所以我不会有共同的方式。
任何帮助表示赞赏。
答案 0 :(得分:1)
我建议使用ASIHTTPRequest或AFNetworking
两者都有一个可以使用的网络队列。 ASIHTTP已不再开发,因此如果您想使用正在开发的库,您可以查看AFNetworking ...
答案 1 :(得分:1)
您应该使用ASIHTTPRequest库。
然后创建自己的类来管理您想要的所有请求。这是我的:
#import "ASIFormDataRequest.h"
@interface PostRequest : 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 "PostRequest.h"
@implementation PostRequest
@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
要使用它,只需在PostRequest.h
中导入UIViewController
并执行以下操作:
[requestManager performGetRequestWithString:tempString stringDictionary:stringDictionary dataDictionary:dataDictionary delegate:self requestSelector:@selector(requestSucceeded:) errorSelector:@selector(requestFailed:)];
参数:
(NSString *)string
- url字符串,发布请求的位置; (NSDictionary *)stringDictionary
- 字典,包含所有文本信息(例如名称,ID等); (NSDictionary *)dataDictionary
- 字典,包含所有数据信息(如照片,文件等); (id)requestDelegate
- 委托执行以下选择器; (SEL)requestSelector
- 选择器,将在成功请求时执行; (SEL)errorSelector
- 选择器,会在发生错误时执行。