gpt4 book ai didi

iphone - 使用Post方法+iphone解析xml

转载 作者:行者123 更新时间:2023-12-03 21:18:37 26 4
gpt4 key购买 nike

我在使用 post 方法进行 xmlparsing 时遇到问题。

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

现在我正在使用:-

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

但是我应该如何在其中制作帖子 HTMLbody 部分..

意味着我应该在代码中的何处以及如何放置函数名称和示例 xml

我编辑的问题是:-

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

但是我的 react 变黑了。请帮帮我..是我的

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

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

这部分代码是否正确?

最佳答案

您可以使用方法 - (void)setHTTPBody:(NSData *)data 设置 html body。例如:

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

在您的情况下,消息是 xml。

您还需要添加此代码来帮助服务器确定发送数据的类型:

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

如果你想为你的请求设置一些额外的标志,你可以使用方法- (void)setValue:(NSString *)value forHTTPHeaderField:(NSString *)field

关于iphone - 使用Post方法+iphone解析xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7495441/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com