1

我有一个与 API 通信的实用程序方法,它使用 POST HTTP 请求进行通信,在我的实用程序类中,我有一个名为:

(void)makeConnectionWithParameters:(NSMutableDictionary*)parameters;

它接受参数并设置 POST 的主体。但是,在一种特殊情况下,我想上传一些图像并且代码稍作更改以使上传所述图像成为可能,这种情况下的最佳做法是什么?我应该将方法重命名为:

(void)makeConnectionWithParameters:(NSMutableDictionary*)parameters andImages(NSArray*)images;

并在所有其他情况下将 nil 设置为参数,或者我应该在调用“makeConnectionWithParameters”的方法中设置一个 bool 并检查该方法是否设置了 bool 并在这种情况下处理图像?

还有什么其他的想法可以让代码更漂亮吗?

这是方法:

(void)makeConnectionWithParameters:(NSMutableDictionary*)parameters
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    NSURL *url = [NSURL URLWithString:BASE_URL];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageAllowed timeoutInterval:20];
    request.HTTPMethod = @"POST";

    NSString *boundary = @"myR4ND0Mboundary";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
    [request setValue:contentType forHTTPHeaderField: @"Content-Type"];

    //Lägg till inloggningsuppgifter för API-anropet
    [parameters setValue:API_LOGIN forKey:@"api-login"];
    [parameters setValue:API_PASSWORD forKey:@"api-password"];

    //Lägg till alla parameterar i POST-bodyn
    NSMutableData *body = [NSMutableData data];

    for (NSString *param in parameters) 
    {
        [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"%@\r\n", [parameters objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
    }

    if(hasImages)
    {
        int c = 0;
        for(UIImage* image in self.images)
        {
            c++;
            NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
            if (imageData) 
            {
                [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
                [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", [NSString stringWithFormat:@"image%d", c]] dataUsingEncoding:NSUTF8StringEncoding]];
                [body appendData:[[NSString stringWithString:@"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
                [body appendData:imageData];
                [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
            }
            imageData = nil;
        }
    }

    //Sätt content-length
    NSString *postLength = [NSString stringWithFormat:@"%d", [body length]];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:body];
    NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];

    if(connection)
    {
        receivedData = [NSMutableData dataWithLength:0];
    }
}

我希望这一切都有意义。:)

4

1 回答 1

1

只需创建两个方法来生成请求对象,而不是实际的连接:

(NSMutableURLRequest*)makeConnectionWithParameters:(NSMutableDictionary*)parameters;
(NSMutableURLRequest*)makeConnectionWithParameters:(NSMutableDictionary*)parameters andImages(NSArray*)images;

在第一个方法调用内部:

[self makeConnectionWithParameters:parameters andImages:nil];

...并对第二种方法中的图像进行 nil 检查。使用这两种方法获取您的请求,然后使用返回的对象创建 NSURLConnection。

于 2012-04-24T16:35:44.307 回答