如何向 JSON Web 服务调用添加 URL 参数 - Objective C

发布于 2025-01-08 17:35:27 字数 803 浏览 0 评论 0原文

我正在尝试使用 SBJsonParser API 使用此代码从 JSON Web 服务获取信息:

 SBJsonParser *parser = [[SBJsonParser alloc] init];

// Prepare URL request to download statuses from Twitter
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://twitter.com/statuses/public_timeline.json"]];

// Perform request and get JSON back as a NSData object
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

// Get JSON as a NSString from NSData response
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

但我需要向 Web 服务 URL 添加两个参数(用户和密码)及其值,因此 URL 将是“http:// /twitter.com/statuses/public_timeline.json?user=name&password=test”。我已经搜索了有关 API 的信息来执行此操作,但没有成功。

非常感谢您的帮助。

I'm trying to fetch information from a JSON web service with this code, using SBJsonParser API:

 SBJsonParser *parser = [[SBJsonParser alloc] init];

// Prepare URL request to download statuses from Twitter
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://twitter.com/statuses/public_timeline.json"]];

// Perform request and get JSON back as a NSData object
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

// Get JSON as a NSString from NSData response
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

But I need to add to the web service URL two parameters, user and password, with their values, so the URL will be "http://twitter.com/statuses/public_timeline.json?user=name&password=test". I have searched information about APIs to do it but I wasn't successful.

Many thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暗恋未遂 2025-01-15 17:35:27
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://twitter.com/statuses/public_timeline.json?user=%@&password=%@", @"name", @"test"]]];

根据需要用您自己的变量替换 @"name"@"test"

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://twitter.com/statuses/public_timeline.json?user=%@&password=%@", @"name", @"test"]]];

Substitute your own variables for @"name" and @"test" as needed.

空心空情空意 2025-01-15 17:35:27

我假设你有变量的值,对吗?如果是这样,只需使用 NSString 的 stringWithFormat: 方法即可。

替换:

@"http://twitter.com/statuses/public_timeline.json"

为:

[NSString stringWithFormat:@"http://twitter.com/statuses/public_timeline.json?user=%@&password=%@", user, password]

I assume you have the values in variables, correct? If so, just use the stringWithFormat: method of NSString.

Replace:

@"http://twitter.com/statuses/public_timeline.json"

With:

[NSString stringWithFormat:@"http://twitter.com/statuses/public_timeline.json?user=%@&password=%@", user, password]
恰似旧人归 2025-01-15 17:35:27
NSString *user = @"user";
NSString *password = @"password";

NSString *urlStr = [NSString stringWithFormat:@"http://twitter.com/statuses/public_timeline.json?user=%@&password=%@", user, password];

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: urlStr]];
NSString *user = @"user";
NSString *password = @"password";

NSString *urlStr = [NSString stringWithFormat:@"http://twitter.com/statuses/public_timeline.json?user=%@&password=%@", user, password];

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: urlStr]];
抚你发端 2025-01-15 17:35:27

你也可以这样做。

 NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc] initWithURL:@"http://twitter.com/statuses/public_timeline.json"];

 [theRequest setHTTPMethod:"@POST"] // Or get, push, put

 NSString theRquest_Body = [NSString stringWithFormat:@"?user=%@&password=%@",
                           [user stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                           [password stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]],

 [theRequest setHTTPBody:[theRequest_body dataUsingEncoding:NSUTF8StringEncoding]];

You could also do it this way.

 NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc] initWithURL:@"http://twitter.com/statuses/public_timeline.json"];

 [theRequest setHTTPMethod:"@POST"] // Or get, push, put

 NSString theRquest_Body = [NSString stringWithFormat:@"?user=%@&password=%@",
                           [user stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                           [password stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]],

 [theRequest setHTTPBody:[theRequest_body dataUsingEncoding:NSUTF8StringEncoding]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文