xcode AFNetworking POST 并取回数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11863817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:10:51  来源:igfitidea点击:

AFNetworking POST and get Data back

iphoneiosxcodepostafnetworking

提问by Chris

in my Iphone app i use AFNetworking to POST some data to a webservice and get some data back...

在我的 Iphone 应用程序中,我使用 AFNetworking 将一些数据发布到网络服务并取回一些数据......

Here is my example, i always get "false" back as response, what i am doing wrong?

这是我的例子,我总是得到“假”作为回应,我做错了什么?

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];

EDIT: the method i call in the Url returns a string (no the string is not false)

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



  [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;
           }

        }

this method i am calling with my iphone and normaly it should return xml or?

我用我的 iphone 调用这个方法,通常它应该返回 xml 还是?

回答by rckoenes

You are using a complicated way of building the operation, but it will work. But it should work, the thing you are missing is assign the XMLparser. In the documentation of AFXMLRequestOperationis stated.

您正在使用一种复杂的方法来构建操作,但它会起作用。但它应该可以工作,您缺少的是分配 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]);
}];

Also there is not need to make the NSURLRequest via the AFHTTPClient, you can easily create one you self or use the AFHTTPClientto do the creation of the AFHTTPRequestOperationfor you.

也不需要通过 来创建 NSURLRequest AFHTTPClient,您可以轻松地自己创建一个或使用AFHTTPClientAFHTTPRequestOperation为您创建。



To use the AFHTTPClient to just return whatever the server returns :

要使用 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);
}];

回答by ElasticThoughts

Do you have a typo here? [udid copy], @"uuid",

你这里有错别字吗? [udid copy], @"uuid",