AFNetworking POST并获取数据

Chr*_*ris 1 iphone post xcode ios afnetworking

在我的iPhone应用程序中,我使用AFNetworking将一些数据发送到Web服务并获取一些数据...

这是我的例子,我总是把"假"作为回应,我做错了什么?

NSURL *baseURL = [NSURL URLWithString:@"http://myPath/Iphone/method"];


    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
    [httpClient defaultValueForHeader:@"Accept"];

    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                            [udid copy], @"uuid",
                            nil];


    NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"http://myPath/Iphone/method" parameters:params];


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


    [httpClient registerHTTPOperationClass:[AFXMLRequestOperation class]];


    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        response = [operation responseString];
        NSLog(@"response: %@",response);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error: %@", [operation error]);
    }];


    [operation start];
Run Code Online (Sandbox Code Playgroud)

编辑:我在Url中调用的方法返回一个字符串(没有字符串不是false)


  [HttpPost]
        public bool checkIphone(string uuid)
        {
           IDictionary<string,object> check  = Request.Properties;

           uuid = check["uuid"].ToString();

           //do anything with the uuid

           if (1<0)
           {
               return true;
           }
           else
           {
               return false;
           }

        }
Run Code Online (Sandbox Code Playgroud)

这个方法我用我的iphone和normaly调用它应该返回xml或?

rck*_*nes 5

您正在使用一种复杂的方法来构建操作,但它会起作用.但它应该工作,你缺少的是分配XMLparser.在AFXMLRequestOperation的文档中说明.

 NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"http://myPath/Iphone/method" parameters:params];

AFXMLRequestOperation *operation = [[AFXMLRequestOperation alloc] initWithRequest:request];
operation.responseXMLParser = xmlParser; //Here you should assign an instance of NSXMLParser to handle you XML parsing.

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    response = [operation responseString];
    NSLog(@"response: %@",response);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"error: %@", [operation error]);
}];
Run Code Online (Sandbox Code Playgroud)

另外也不需要通过NSURLRequest来制作AFHTTPClient,你可以轻松地创建一个你自己或者用来为你AFHTTPClient做创作AFHTTPRequestOperation.


要使用AFHTTPClient返回服务器返回的内容:

// Don't include the method here
NSURL *baseURL = [NSURL URLWithString:@"http://myPath/Iphone/"];

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
[httpClient defaultValueForHeader:@"Accept"];

NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                        [udid copy], @"uuid",
                        nil];

[httpClient postPath:@"method" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
    // reponseObject will hold the data returned by the server.

}failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error retrieving data: %@", error);
}];
Run Code Online (Sandbox Code Playgroud)