Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 822405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:47:40+00:00 2026-05-15T02:47:40+00:00

I have this code NSString *tr = [self sendUrl:@http://google.com/]; But for some reason ‘

  • 0

I have this code

   NSString *tr = [self sendUrl:@"http://google.com/"];

But for some reason ‘tr‘ will remain nil after it is executed. What am I doing wrong?

sendUrl :

- (NSString *)sendUrl:(NSString *) uri {

NSLog(@"Requesting URI 1 ...");

// Prepare URL request to download statuses from Twitter
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:uri]];

NSLog(@"Requesting URI 2 ...");

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

NSLog(@"Requesting URI 3 ...");

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

NSLog(@"Requesting URI 4 ...");

return json_string;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T02:47:41+00:00Added an answer on May 15, 2026 at 2:47 am

    Are you absolutely certain that response is not nil? If your request to Google or wherever fails, response will be set to nil and the error will contain some information that will help you diagnose the error so change

    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    

    to

    NSError* error = nil;
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
    if (response == nil)
    {
        NSLog(@"request failed with error %@", error);
        // any other error handling
    }
    

    Next thing. The default encoding for an HTTP message is ISO-8859-1, not UTF-8. According to Apple’s docs, -initWithData: will return nil if the encoding is wrong. You probably want NSISOLatin1StringEncoding. I say “probably” because HTTP has a mechanism for telling you what character encoding it used. I think it’s the header Content-Transfer-Encoding but I advise you to Google the HTTP RFC to find out for sure.

    Finally if json_string were not nil, it would be leaking. Because you obtained it with alloc, you own it which means you need to autorelease it before returning it from sendURL: This last point is not the cause of your problem, it is a separate bug in your code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have this piece of code: NSString *strURL = [NSString stringWithFormat:@http://www.sample.com /phpFile.php?firstname=%@,txtfirstName.text]; NSString
I have this code self.text = [NSSTring stringWithUTF8String:(char *)sqlite_column_text(init_statement, 0)]; I'm wondering why we
I have this line of code: NSString *dateString = [self.dates objectAtIndex:row]; that gets set
I have this code: NSString *path = [NSString stringWithFormat:@%@/%@, [[NSBundle mainBundle] resourcePath], @change.mp3]; NSURL
i have an error in this code NSString *str = @above string; //load above
In viewDidLoad, I have this code: ProvRec *provRec = [[ProvRec alloc]init]; provRec.status = [NSString
I have a NSString that is a date and time in this code: YYYY-MM-DD
I have this code to change the number in the NSString every five seconds.
In my viewController.m I have this code: self.movie = [[myMovie alloc]init]; self.movie.name = @Iron
So, I have this code: timer = [NSTimer scheduledTimerWithTimeInterval:4.0 target:self selector:@selector(a) userInfo:nil repeats:YES]; This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.