如何向 JSON Web 服务调用添加 URL 参数 - Objective C
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据需要用您自己的变量替换
@"name"
和@"test"
。Substitute your own variables for
@"name"
and@"test"
as needed.我假设你有变量的值,对吗?如果是这样,只需使用 NSString 的 stringWithFormat: 方法即可。
替换:
为:
I assume you have the values in variables, correct? If so, just use the stringWithFormat: method of NSString.
Replace:
With:
你也可以这样做。
You could also do it this way.