1

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.

4 Answers 4

3
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.

Sign up to request clarification or add additional context in comments.

Comments

2

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]

1 Comment

-1 The other answers are correct: For example:[NSString stringWithFormat : @"http:\//twitter.com/statuses/public_timeline.json?user=%@&password=%@", @"user", @"pas=?/word"] would result in: "http:\//twitter.com/statuses/public_timeline.json?user=user&password=pas=?/word" There is no proper URL escaping in this solution.
2
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]];

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.