开发者

xml parsing with Post method+iphone

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开发者_如何学C:@"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??


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜