MKNetworkKit如何追加帖子数据

时间:2013-01-17 11:39:44

标签: mknetworkkit

我目前正在尝试从ASIHttpRequest切换到MKNetworkKit,而且我的帖子数据有问题。如何在MKNetworkKit中执行以下操作?

ASIHTTPRequest * request = [ASIHTTPRequest requestWithURL:url];

[request appendPostData:[[NSString stringWithFormat:@“{\”version \“:%@,\”values \“:[{\”device \“:\”%@ \“,\”os \“ :\“ios \”,\“version \”:\“%@ \”}}}}“,VERSION,[[UIDevice currentDevice] uniqueIdentifier],[[UIDevice currentDevice] systemVersion]] dataUsingEncoding:NSUTF8StringEncoding]];

我只是不确定如何在MKNetworkKit中附加帖子数据。我找到了addData方法,但是这不允许我添加上面的数据以及值{}中的额外数据。

有谁知道怎么做?

2 个答案:

答案 0 :(得分:0)

试试这个:

MKNetworkOperation *op = [self operationWithPath:INIT_URL params:body httpMethod:@"POST"    ssl:YES];
[op setPostDataEncoding:MKNKPostDataEncodingTypeJSON];//This is important
[op addCompletionHandler:^(MKNetworkOperation *completedOperation)

答案 1 :(得分:-1)

虽然我是新手,但是让我与你分享我在做什么。

首先,我创建了引擎(我调用的服务所在的位置)。

在我的界面中:

@interface INGMainViewController (){
    MKNetworkEngine *engine_;

}

在我的初始化中:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

  {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            engine_= [[MKNetworkEngine alloc] initWithHostName:@"www.xxx.gr"];
            // Custom initialization
        }
        return self;
    }

然后在我将调用该服务的函数中:

NSDictionary *formParams = [NSDictionary dictionaryWithObjectsAndKeys:
                            theClientCode, @"ClientCode",
                            theContractNumber, @"ContractCode",
                            theAppleID, @"AppleID",
                            @" ", @"AndroidID",
                            @" ", @"WindowsID",
                            uid, @"AppleDeviceID",
                            @" ", @"AndroidDeviceID",
                            @" ", @"WindowsDeviceID",
                            theCellPhone, @"TelephoneNumber"
                            , nil];

    MKNetworkOperation *operation = [self.engine operationWithPath:@"/services/Authentication.ashx"
                                                            params: formParams
                                                        httpMethod:@"POST"];


    [operation addCompletionHandler:
     ^(MKNetworkOperation *operation)
     {
         NSLog(@" Operation succeds: %@", [operation responseString]);
     }
                       errorHandler:^(MKNetworkOperation *errorOperation, NSError *error)
                        {
                           NSLog(@" Errors occured: %@", error);
                        }];
    [self.engine enqueueOperation:operation];
}

......就是这样。

当然我仍然面临一些问题,但我认为没有来自Kit。

我希望我能帮到你。