0

我目前正在尝试从 ASIHttpRequest 切换到 MKNetworkKit,并且我在发布数据时遇到问题。如何在 MKNetworkKit 中执行以下操作?

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];

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

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

有谁知道怎么做?

4

2 回答 2

0

尝试这个:

MKNetworkOperation *op = [self operationWithPath:INIT_URL params:body httpMethod:@"POST"    ssl:YES];
[op setPostDataEncoding:MKNKPostDataEncodingTypeJSON];//This is important
[op addCompletionHandler:^(MKNetworkOperation *completedOperation)
于 2013-11-29T17:26:21.277 回答
-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];
}

……仅此而已。

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

我希望我对你有所帮助。

于 2013-04-11T09:56:48.493 回答