5

私はAFNetworkingを使用してPOSTjsonを使用しようとしています。いくつかの調査の後、私はついにこれを機能させることができました

- (IBAction)go:(id)sender {
    //Some POST
    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"https://www.test.com/"]];
    [httpClient setParameterEncoding:AFJSONParameterEncoding];
    NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST"
                                                            path:@"https://www.test.com/user/login/"
                                                      parameters:@{@"username":self.username.text, @"password":self.password.text}];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        // Print the response body in text
        NSDictionary *jsonDict = (NSDictionary *) responseObject;
        BOOL *success = [[jsonDict objectForKey:@"success"] boolValue];
        NSLog(@"Response: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
    [operation start];
}

これはデモンストレーションの目的には問題ありませんが、最終的にNSDirectoryを使用してjsonオブジェクトを分割したいので、このようなことを試みましたが、ボタンをクリックするたびにアプリがクラッシュします

- (IBAction)go:(id)sender {
    //Some POST
    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"https://www.test.com/"]];
    [httpClient setParameterEncoding:AFJSONParameterEncoding];
    NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST"
                                                            path:@"https://www.test.com/user/login/"
                                                      parameters:@{@"username":self.username.text, @"password":self.password.text}];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        // Print the response body in text
        NSDictionary *jsonDict = (NSDictionary *) responseObject;
        BOOL success = [[jsonDict objectForKey:@"success"] boolValue];
        if (success) {
            NSLog(@"yes!!!");
        }else{
            NSString *reason = [jsonDict objectForKey:@"reason"];
            NSLog(@"reason: %@",reason);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
    [operation start];
}

エラー

2013-03-08 03:27:34.648 test[18253:c07] -[__NSCFData objectForKey:]: unrecognized selector sent to instance 0x888a6b0
2013-03-08 03:27:34.648 test[18253:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFData objectForKey:]: unrecognized selector sent to instance 0x888a6b0'
*** First throw call stack:
(0x1ce0012 0x111de7e 0x1d6b4bd 0x1ccfbbc 0x1ccf94e 0x24608 0x12e71 0x4a4453f 0x4a56014 0x4a467d5 0x1c86af5 0x1c85f44 0x1c85e1b 0x1c3a7e3 0x1c3a668 0x61ffc 0x21ad 0x20d5)
libc++abi.dylib: terminate called throwing an exception
4

4 に答える 4

14

使用していて、使用AFHTTPRequestOperationしていませんAFJSONRequestOperation

AFHttpClientまた、直接使用することもできます。

NSDictionary *parameter = @{@"username":self.username.text, @"password":self.password.text};
[httpClient setParameterEncoding:AFJSONParameterEncoding];
[httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];

[httpClient postPath:@"api/v1/user/login/" parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
    // Print the response body in text
    BOOL *success = [[responseObject objectForKey:@"success"] boolValue];
    NSLog(@"Response: %@",responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    [self handleConnectionError:error];
}];

AFHTTPClientまた、 usingclientWithBaseURL:メソッドのインスタンスを1つだけ作成することをお勧めします。

于 2013-03-08T09:38:43.863 に答える
0

JSONオブジェクトを送信し、JSONデータを取得して解析しようとしていました

NSURL *url = [NSURL URLWithString:@"https://MySite.com/"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc]initWithBaseURL:url];
//[httpClient setParameterEncoding:AFJSONParameterEncoding];
//[httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];

NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                        @"Ans", @"name",
                        @"29",  @"age", nil];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST"
                                                        path:@"/testJSONReturn.php"
                                                  parameters:params];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    NSLog(@"DATA: %@", [JSON valueForKeyPath:@"data"]);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
    NSLog(@"Request Failure Because %@",[error userInfo]);
}];

[operation start];

役立つかもしれません

于 2013-09-26T06:41:40.953 に答える
0

AFHTTPRequestOperationManagerを使用すると、リクエストマネージャーを追加できます。私の場合は、パラメーターとしてNSdictionnaryを使用してPOSTメソッドを実行しました。

[manager GET:url parameters:parameters success:success failure:failure];

この行を使用して、メソッド/GETまたはPOSTを選択できます

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters= dic;
NSMutableString *url = [[NSMutableString alloc] init];
[url appendString: RootObj.root];
    [url appendString:@"/yourURL/"];
manager.requestSerializer=[AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];

[manager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {      
}];
}
于 2015-02-04T15:17:17.967 に答える
-1

AFHTTPRequestOperationを使用する別のメソッド:

NSString* path = "http://www.test.com/file.php";
NSMutableURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:path]];
//request.HTTPMethod = @"GET";
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
//operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"success: %@", operation.responseString);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"error: %@",  operation.responseString);
}];
[operation start];
于 2014-03-22T10:17:44.940 に答える