使用AFNetworking将文本和多个图像发布到Google Blobstore

时间:2022-03-15 23:11:29

For reference, here is the working android/Java version of what I am trying to do in iOS/Objective-c

作为参考,这是我在iOS / Objective-c中尝试做的工作的android / Java版本

public static void saveTextsAndImagesOnServer(List<byte[]> images, long someID1, String servingUrl, boolean someFlag)
            throws ClientProtocolException, IOException {
        Log.d(TAG, "saveTextsAndImagesOnServer started ");
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost(servingUrl);
        MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

        AdditionData extr = AdditionData.getInstance();
        reqEntity.addPart("red", new ByteArrayBody(("" + extr.getred()).getBytes(), "red"));
        reqEntity.addPart("yellow", new ByteArrayBody(extr.getyellow.getBytes(), "yellow"));
        reqEntity.addPart("green", new ByteArrayBody(extr.getgreen().getBytes(), "green"));
        reqEntity.addPart("blue", new ByteArrayBody((extr.getblue()).getBytes(), "blue"));
        reqEntity.addPart("someID1", new ByteArrayBody(("" + someID1).getBytes(), "someID1"));
        if (someFlag) {
            reqEntity.addPart("someFlag", new ByteArrayBody("true".getBytes(), "someFlag"));
        }
        int i = 0;
        for (byte[] img : images) {
            ByteArrayBody image = new ByteArrayBody(img, "img" + i++ + ".png");
            reqEntity.addPart("image", image);
        }
        postRequest.setEntity(reqEntity);
        HttpResponse response = httpClient.execute(postRequest);
        Log.d(TAG, "saveTextsAndImagesOnServer ended with response " + response.toString());
    }

summary: Basically I am able to send an image and accompanying metadata to the blobstore. The metadata helps me identify, for example, who sent the image.

摘要:基本上我能够将图像和附带的元数据发送到blobstore。例如,元数据可以帮助我识别发送图像的人。

Now trying to do the same thing in iOS I wrote the code below. But for whatever reason, the metadata is not been saved in the blobstore. I try a bunch of different things but I am still getting nothing. I try changing my metadata strings to base-64 but that didn’t work.

现在尝试在iOS中做同样的事情,我编写了下面的代码。但无论出于何种原因,元数据都没有保存在blobstore中。我尝试了一堆不同的东西,但我仍然一无所获。我尝试将我的元数据字符串更改为base-64,但这不起作用。

THE IOS CODE

IOS代码

-(void)postMultipartToServer
{
    if (!self.destinationUrl) {
        return;
    }

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSLog(@"IS dictionary empty? %@", self.textDictionary);

    [manager POST:self.destinationUrl
       parameters:self.textDictionary
    constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        if ([self.imageDictionaries count]>0)
            for (NSDictionary *imgDic in self.imageDictionaries) {
                [formData appendPartWithFileData:UIImagePNGRepresentation([imgDic objectForKey:@"image"])
                                            name:[imgDic objectForKey:@"name"]//@"image"
                                        fileName:[imgDic objectForKey:@"fileName"]//@"image.png"
                                        mimeType:[imgDic objectForKey:@"mimeType"]//@"image/png"
                 ];
            }


    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Success: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
}

Here is the dictionary

这是字典

 NSDictionary *textDictionary = @{
                                 @"yellow”:self. yellow,
                                 @"red":self. red,
                                 @"green”:self.green,
                                 @“blue”:self. blue,
                                 @“spouse”:self.spouse,
                                 @"isFamouse”:@”true”};//you get the idea

Where am I?

我在哪里?

Given that this design works on android, I know it’s possible especially since http is supposed to be agnostic when it comes to languages. So the problem is reduced to saving string from iOS to the Google blobstore.

鉴于这个设计适用于android,我知道这是可能的,特别是因为http在语言方面应该是不可知的。所以问题就是将字符串从iOS保存到Google blobstore。

I am starting a bounty in hope someone will help me resolve this issue (perhaps a google blobstore-iOS expert). I also noticed other people have asked fairly the same question, but no answer. Other ways I have asked this question:

我正在开始赏金,希望有人能帮我解决这个问题(也许是谷歌blobstore-iOS专家)。我也注意到其他人提出了相同的问题,但没有回答。我提出这个问题的其他方式:

AFHTTPRequestOperationManager post multi-part request not working

AFHTTPRequestOperationManager发布多部分请求不起作用

How to convert NSString to UIImage for appengine blobstore

如何将NSString转换为Ungmage for appengine blobstore

2 个解决方案

#1


2  

Replace the method as follows

替换方法如下

-(void)postMultipartToServer
{
    if (!self.destinationUrl) {
        return;
    }

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    [manager POST:self.destinationUrl
       parameters:nil
    constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

        if ([self.textDictionary count]>0){
            if ([self.textDictionary count]>0)
                [self.textDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id object, BOOL *stop) {
                    [formData appendPartWithFileData:[object dataUsingEncoding:NSUTF8StringEncoding]
                                                name:key
                                            fileName:key
                                            mimeType:@"application/json"
                     ];

                }];
        }

        if ([self.imageDictionaries count]>0)
            for (NSDictionary *imgDic in self.imageDictionaries) {
                [formData appendPartWithFileData:UIImagePNGRepresentation([imgDic objectForKey:@"image"])
                                            name:[imgDic objectForKey:@"name"]//@"image"
                                        fileName:[imgDic objectForKey:@"fileName"]//@"image.png"
                                        mimeType:[imgDic objectForKey:@"mimeType"]//@"image/png"
                 ];
            }
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Success: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
}

#2


0  

In the end the http request will be a string. You could log that string. Probably want to use a small image for that. Then do the same for the android request and see what the difference is.

最后,http请求将是一个字符串。您可以记录该字符串。可能想要使用一个小图像。然后为Android请求做同样的事情,看看有什么区别。

How to print AFNetworking request as RAW data

如何将AFNetworking请求打印为RAW数据

#1


2  

Replace the method as follows

替换方法如下

-(void)postMultipartToServer
{
    if (!self.destinationUrl) {
        return;
    }

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    [manager POST:self.destinationUrl
       parameters:nil
    constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

        if ([self.textDictionary count]>0){
            if ([self.textDictionary count]>0)
                [self.textDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id object, BOOL *stop) {
                    [formData appendPartWithFileData:[object dataUsingEncoding:NSUTF8StringEncoding]
                                                name:key
                                            fileName:key
                                            mimeType:@"application/json"
                     ];

                }];
        }

        if ([self.imageDictionaries count]>0)
            for (NSDictionary *imgDic in self.imageDictionaries) {
                [formData appendPartWithFileData:UIImagePNGRepresentation([imgDic objectForKey:@"image"])
                                            name:[imgDic objectForKey:@"name"]//@"image"
                                        fileName:[imgDic objectForKey:@"fileName"]//@"image.png"
                                        mimeType:[imgDic objectForKey:@"mimeType"]//@"image/png"
                 ];
            }
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Success: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
}

#2


0  

In the end the http request will be a string. You could log that string. Probably want to use a small image for that. Then do the same for the android request and see what the difference is.

最后,http请求将是一个字符串。您可以记录该字符串。可能想要使用一个小图像。然后为Android请求做同样的事情,看看有什么区别。

How to print AFNetworking request as RAW data

如何将AFNetworking请求打印为RAW数据