使用AFNetworking将文本和多个图像发布到Google Blobstore [英] Using AFNetworking to post both text and multiple images to Google Blobstore

查看:197
本文介绍了使用AFNetworking将文本和多个图像发布到Google Blobstore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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());
    }

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

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.

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

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代码

-(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);
    }];
}

这是字典

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

我在哪儿?

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

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.

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

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:

AFHTTPRequestOperationManager发布多部分请求无效

如何将NSString转换为Ungmage for appengine blobstore

推荐答案

将方法替换如下

-(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);
    }];
}

这篇关于使用AFNetworking将文本和多个图像发布到Google Blobstore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆