1

I have an custom class object in my cocos2d game and I want to store it on the server, along with PNG image and some id parameters.

Currently this object saved and loaded using NSKeyedArchiver on the actual device or simulator. But I need a server! :p

I'm not familiar with PHP, so I've asked one my friend to create a simple server for me, and now I've some server that can accept requests... so now my turn - upload data.

So, I'm trying to find a simple solution and came with AFNetworking, however I can't find useful example that help me out.

My question is: how to send my custom object to server along with parameters:

  • Id (NSString),
  • userName (NSString,
  • image (png file save locally on the device)

And server can store this data and provide me id or link to this object (this part I believe must be done on a server side), so I can download it from server later and use this data in my game.

EDIT: I'm assuming, request would be formed from 4 "parts":

  • custom object(NSData)
  • id (NSString)
  • userName (NSString)
  • image file (local png file)

But, how to make all these in one request and send? Also, after that, I will send request to server like just GET: www.server.com/id and server will return me:

  • my custom object(NSData)
  • userName (NSString)
  • PNG image file
4

2 回答 2

2

好的,我再次阅读了有关 AFNetworking 的常见问题解答,但对我来说 - 这是正常示例的泄漏.. 所以我再次尝试使用谷歌并找到了一些东西......另外,你提供的代码,我也已经测试过,它有效,但我需要发送一个文件,而不仅仅是参数。

所以这是我按预期工作的完整代码。

    NSData *sendData = [NSKeyedArchiver archivedDataWithRootObject:self.customGameObject];           
    NSURL *remoteUrl = [NSURL URLWithString:@"http://blablabla.com"];

    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:remoteUrl];
    NSMutableURLRequest *afRequest = [httpClient multipartFormRequestWithMethod:@"POST"
                                                                           path:@"/api.php"
                                                                     parameters:nil
                                                      constructingBodyWithBlock:^(id <AFMultipartFormData>formData)
      {

          [formData appendPartWithFormData:[@"saveObject" dataUsingEncoding:NSUTF8StringEncoding] name:@"action"];
          NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
          NSString *documentsDirectory = [paths objectAtIndex:0];
          NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Icon.png"]; 

          NSData * photoImageData = UIImagePNGRepresentation([UIImage imageNamed:@"Icon.png"]);
          [formData appendPartWithFileData:photoImageData
                                      name:@"Image"
                                  fileName:filePath
                                  mimeType:@"image/png"];
          [formData appendPartWithFormData:sendData name:@"objectData"];
      }
    ];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:afRequest];

    [operation setCompletionBlock:^{
        NSLog(@"response: %@", operation.response); 
        NSLog(@"responseString: %@", operation.responseString); 
        NSLog(@"responseData: %@", operation.responseData); 
    }];

    [operation start];

另外,我已经使用相同的请求成功地取回了我的对象,但是使用参数 - “getObject”="id.." id - 服务器在收到请求时返回 saveObject

感谢您之前的回答,如果我做错了什么,请澄清..

于 2013-07-26T01:28:11.670 回答
0

你的问题很广泛,涵盖了很多东西。我会给你一个基本的概述,当你遇到问题时,你可以发布更具体的问题(如果你无法弄清楚的话。)

首先,请完整阅读AFNetworking入门指南常见问题解答。相信我,它会为你节省大量的时间。

现在,让您拥有一个 AFHTTPClient 的实例(可能是一个单例子类)。使用正确的参数编码对其进行设置。AFNetworking 默认为 Form URL 参数编码,但也支持 Plists 和 JSON。您的服务器人员应该知道这是正确的。像这样设置它:

myHTTPClient.parameterEncoding = AFJSONParameterEncoding;

以下是发布一些参数的方法:

NSString *path = "api/v1/addUser"; // path to your resource
NSDictionary *params = @{@"id" : myIDString, @"userName" : myUserNameString};

[myHTTPClient postPath:path parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Success! \n\n %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Failure! \n\n %@", error);
}];

只要服务器响应并且客户端完成处理您的请求,就会调用完成块。此时,您可以处理数据、移动到新视图、继续通过登录页面或任何您想要的。

如果这不能为您提供足够的控制,请查看该[AFHTTPClient -postPath:parameters:success:failure:]方法的实现。

注意:您应该始终检查失败块中的erroroperation变量。可以出于多种原因进行设置,例如无效数据、404(未找到文件)或连接超时。您可能希望根据原因做出不同的反应。

最后,我不会讨论上传照片,因为我在上面链接的常见问题解答中概述了确切的步骤。

于 2013-07-25T03:13:21.883 回答