【问题标题】:AFNetworking POST and get Data backAFNetworking POST 并取回数据
【发布时间】:2012-08-08 11:46:49
【问题描述】:

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

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

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

编辑:我在 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;
           }

        }

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

【问题讨论】:

标签: iphone ios xcode post afnetworking


【解决方案1】:

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

另外,也不需要通过AFHTTPClient创建NSURLRequest,您可以轻松创建一个自己或使用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);
}];

【讨论】:

  • 我不使用 NSXMLParser 我使用 RaptureXML 解析器
  • 不要使用AFXMLRequestOperation ,也不要将其设置为AFHTTPClient中的默认类,只需使用普通的AFHTTPRequestOperation并将responseObject粘贴到RaptureXML 跨度>
  • [httpClient postPath:@"method" params success:^(AFHTTPRequestOperation *operation, id responseObject) {} 这行有问题,在这种情况下成功代表什么?
  • 当我想打印 responseObject 时是否正确? RXMLElement *rxml = [RXMLElement elementFromXMLFile:responseObject]; NSLog(@"%@", rxml);
  • 是的,我确定,因为基本 URL 是在 AFHTTPClient 上设置的,它会将方法附加到 URL。看起来 responseObject 是NSData。试试这个:NSString *responseString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]; NSLog(@"response: %@", responseString);
【解决方案2】:

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

【讨论】:

  • 错字?我认为这只是复制变量
  • udid 是一个 NSString,带有来自 Iphone 的 uuid
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多