1

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

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)


  [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?

casperOne
  • 73,706
  • 19
  • 184
  • 253
Chris
  • 709
  • 2
  • 6
  • 9

2 Answers2

5

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 AFXMLRequestOperation is stated.

 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 AFHTTPClient to do the creation of the AFHTTPRequestOperation for you.


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

// 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);
}];
rckoenes
  • 69,092
  • 8
  • 134
  • 166
  • i do not use NSXMLParser i use the RaptureXML Parser – Chris Aug 08 '12 at 11:57
  • 1
    The don't use `AFXMLRequestOperation ` and don't set it as the default class in the `AFHTTPClient` just use a plain `AFHTTPRequestOperation` and pase the `responseObject` to `RaptureXML ` – rckoenes Aug 08 '12 at 12:00
  • [httpClient postPath:@"method" params success:^(AFHTTPRequestOperation *operation, id responseObject) {} got a problem with this line, what should success stand for here in this case? – Chris Aug 08 '12 at 12:13
  • is this right when i want to print the responseObject? RXMLElement *rxml = [RXMLElement elementFromXMLFile:responseObject]; NSLog(@"%@", rxml); – Chris Aug 08 '12 at 12:23
  • Sorry can't answer that since i've never used `RXMLElement` but you can do NSLog(@"Response: %@", responseObject); – rckoenes Aug 08 '12 at 12:27
  • hmmm then i get back <66616c73 65> i should get back true ore false :( – Chris Aug 08 '12 at 12:30
  • r u sure that the method should not be in the url? when i use the method i a webbrowser i also use the full url: http://anypath/Iphone/anymethod – Chris Aug 08 '12 at 12:32
  • 1
    Yes I'm sure, since the base URL is set on the `AFHTTPClient` and it will append the method to the URL. Looks like the responseObject is a `NSData`. Try this: `NSString *responseString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]; NSLog(@"response: %@", responseString);` – rckoenes Aug 08 '12 at 12:35
  • hmmm now i get false back, but no matter what i return in the method i call it is always false... can nslog print standart xml? because that what i return in the method will be returned as an xml object – Chris Aug 08 '12 at 12:43
  • Xml is just text, so if the `NSLog` prints 'false' then the server isn't returning any XML. – rckoenes Aug 08 '12 at 12:53
  • is that ok ? [udid copy], @"uuid", – Chris Aug 09 '12 at 06:48
  • you saved me!! I realized i have to put the key after its value from your example. Thanks! :'( and :) – trillions Feb 26 '13 at 11:10
-1

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

ElasticThoughts
  • 3,417
  • 8
  • 43
  • 58