Using AFNetworking to post both text and multiple images to Google Blobstore

老子叫甜甜 提交于 2019-12-19 04:45:47

问题


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

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.

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.

THE IOS CODE

-(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.

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 post multi-part request not working

How to convert NSString to UIImage for appengine blobstore


回答1:


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:


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.

How to print AFNetworking request as RAW data



来源:https://stackoverflow.com/questions/24747809/using-afnetworking-to-post-both-text-and-multiple-images-to-google-blobstore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!