2

I am having problem with xmlparsing with post method.

API URL :  http://XXX.XXX.X.XX/api/user.php
Function Name : getUserList
Sample XML :
<root>
    <data>
        <id>0</id>
        <search></search>
    </data>
</root>

Now i am using :-

// setting up the URL to post to
NSString *urlString = @"http://XXX.XXX.X.XX/api/user.php";

// setting up the request object now
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];        

But how should i make the post HTMLbody part in this..

Means where and how should i put functional name and sample xml in the code

My Edited Question is :-

  NSString *urlString = @" http://192.168.6.79/silverAPI/api/user.php/getUserList";

    // setting up the request object now
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];
    NSString * str = @"<root><data><id>0</id><search>a</search></data></root>";
    NSString * message= [NSString stringWithFormat:@"/getUserList mydata=%@", str];
    [request setHTTPBody:[message dataUsingEncoding:NSUTF8StringEncoding]];
    [request addValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];





    // now lets make the connection to the web
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

    NSLog(@"Responce==>%@",returnString);

But i getting black in responce.Pleaseee help me out.. is my

 NSString *urlString = @" http://192.168.6.79/silverAPI/api/user.php/getUserList";

And

NSString * str = @"0a"; NSString * message= [NSString stringWithFormat:@"/getUserList mydata=%@", str]; [request setHTTPBody:[message dataUsingEncoding:NSUTF8StringEncoding]]; [request addValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

This part of code is correct??

BenMorel
  • 34,448
  • 50
  • 182
  • 322
ios developer
  • 3,363
  • 3
  • 51
  • 111

2 Answers2

1

You can set html body using method - (void)setHTTPBody:(NSData *)data. For example:

[request setHTTPBody:[message dataUsingEncoding:NSUTF8StringEncoding]];

where message is,in your case, xml.

Also you need to add this code to help server to determine type of sent data:

[request addValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

If you want to set some additional flags to your request you can use method - (void)setValue:(NSString *)value forHTTPHeaderField:(NSString *)field.

Nekto
  • 17,837
  • 1
  • 55
  • 65
  • Thank for the quick reply.Can u give me the idea how should i get the responce data.I had used this.Please guide me whether it is proper:-NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; – ios developer Sep 21 '11 at 06:45
  • 1
    It looks ok, but why don't you want to check returning response code? – Nekto Sep 21 '11 at 06:59
  • I want to check that whether the responce i m getting throigh this is proper or not – ios developer Sep 21 '11 at 07:11
  • @Nikto Thanks for the reply and help.my request is :-NSString *message=@"getUserList;data=0a"; [request setHTTPBody:[message dataUsingEncoding:NSUTF8StringEncoding]]; I am geting black in responce string.I am should i need to add somthing like this:-[request setValue:nil forHTTPHeaderField:@"Content-Type"]; – ios developer Sep 21 '11 at 07:16
  • Are you sure that your server expects xml in `mydata`? ` NSString * message= [NSString stringWithFormat:@"/getUserList mydata=%@", str];`? And i think, you should remove "/getUserList " – Nekto Sep 21 '11 at 09:51
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/3653/discussion-between-shweta-and-nekto) – ios developer Sep 21 '11 at 10:00
0

What you have done is perfect, please check on server side whether response from there is coming properly by sending the same request or not.

Bitmap
  • 12,402
  • 16
  • 64
  • 91
Jane
  • 288
  • 1
  • 3
  • 18