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 7787793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:47:01+00:00 2026-06-01T20:47:01+00:00

I was wondering why in NSURLResponse in a sendSynchronousRequest would not receive information every

  • 0

I was wondering why in NSURLResponse in a sendSynchronousRequest would not receive information every time even though no change has occurred. It looks like there is NSData being received but it is not getting converted into a string and then not getting converted to an NSArray via my json call. The code is below. The code below is all in a single function but dont feel the name is necessary.

NSURL *searchURL = [NSURL URLWithString:@"website"];
NSString *searchedItem = searchFood.text;
NSString *post = [NSString stringWithFormat:@"a=ajax_food_search3&u_id=*******&value=%@", searchedItem];
NSData *pData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *pLength = [NSString stringWithFormat:@"%d", [pData length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
[request setURL:searchURL];
[request setHTTPMethod:@"POST"];
[request setValue:pLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:pData];
//[request setTimeoutInterval:30];

NSURLResponse *response = nil;
NSError *err = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSString *sData = [NSString stringWithUTF8String:[data bytes]];

NSArray *jsonFoodArr = [sData objectFromJSONString];
if(jsonFoodArr != nil){
    NSDictionary *jsonDic = [jsonFoodArr objectAtIndex: 0];
    NSDictionary *jsonDat = [jsonDic objectForKey:@"dat"];
    NSArray *jsonFoodList = [jsonDat objectForKey:@"file"];

    for (int i = 0; i < [jsonFoodList count]; i++) {
        NSDictionary *fill = [jsonFoodList objectAtIndex:i];
        NSDictionary *foodDat = [fill objectForKey:@"fdat"];
        [foodResults addObject:[foodDat objectForKey:@"f_name"]];
    }

    NSLog(@"%@\n", foodResults);
}else {
    NSLog(@"Failed to read information");
}

I wanted to add that my NSData object gives me the same bytes when the connection fails as well as when it works. I am not sure if it is because they are using the same memory address, which I feel would be completely unlikely for the fact that there is no way for the computer to be that lucky when I restart the app. Either way, I receive no error, I do receive a response and I do receive some sort of data but the NSString is not converting it every time to give me a json that is not nil.

I have found after doing 1000 calls to the server that the string that is coming from the connection converted into an NSString is giving me this when it fails.

    [
          {
            "cmd": null,
            "dat": {
              "file": [
                {
                  "name": "Rob's Doc",
                  "f_id": "6",
                  "user_id": "******",
                  "ts": "1329488441"
                },
                {
                  "name": "Objective C",
                  "f_id": "566",
                  "user_id": "******",
                  "ts": "1328116043"
                },
                {
                  "name": "Football Challenge",
                  "f_id": "314",
                  "user_id": "******",
                  "ts": "1326396493"
                }
              ],
              "view_id": null
            }
          }
        ]
    ll}}]
0=0 Pragma: no-cache Connection: close Transfer-Encoding: chunked Content-Type: text/html
     10c [
      {
        "cmd": null,
        "dat": {
          "file": [
            {
              "name": "**** Doc",
              "f_id": "6",
              "user_id": "******",
              "ts": "1329488441"
            },
            {
              "name": "Objective C",
              "f_id": "566",
              "user_id": "******",
              "ts": "1328116043"
            },
            {
              "name": "Football Challenge",
              "f_id": "314",
              "user_id": "******",
              "ts": "1326396493"
            }
          ],
          "view_id": null
        }
      }
    ] 0

The NSHTTPResponse status code for fail and pass is 200, so the page is loading still as well but this response occurs when I can not retrieve a json. I can make multiple calls via the browser and have this work just fine. Android does not fail to obtain a json in the entire time I was coding with them. So two systems do not give me this response.

ANSWER:

The answer to this problem pertains to the NSString that is being used to convert NSData what should be there is

NSString *sData = [[NSString alloc] initWithData: data encoding: NSUTF8String]

This is because the original way in the code has a chance that the string will try to access and store information from memory addresses outside the size of the data that would still be in NSUTF8String format. When you allocate the string ,the string is only the size of the data and thus will only convert the data to that size and only store the data at that size.

This caused my server response to always have the correct string to then access the JSON objects from. Basically, it was a pointer issue for lack of better words in the sense that the original code was trying to access more then it was supposed to because it was an open ended size and copy and was being a little too greedy alloc will not allow string to be greedy.

  • 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-06-01T20:47:03+00:00Added an answer on June 1, 2026 at 8:47 pm

    ANSWER:

    The answer to this problem pertains to the NSString that is being used to convert NSData what should be there is

    NSString *sData = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]
    

    This is because the original way in the code has a chance that the string will try to access and store information from memory addresses outside the size of the data that would still be in NSUTF8String format. When you alloc the string the string is only the size of the data and thus will only convert the data to that size and only store the data at that size. This caused my server response to always have the correct string to then access the JSON objects from. Basically, it was a pointer issue for lack of better words in the sense that the original code was trying to access more then it was supposed to because it was an open ended size and copy and was being a little too greedy alloc will not allow string to be greedy.

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

Sidebar

Related Questions

Wondering if it would ever be useful to index every possible state of an
Wondering if anyone has a good solution for this. My app is displaying nothing
Wondering if anyone has had any experience using retina assets with the NativeControls iPhone
Wondering what would be the most efficient way to do the following. Game play.
Wondering if there is a way to change the left css value of this
Wondering if anyone else has encountered this problem when utilizing the new ability to
Wondering if anyone out there has ran into this before.... I'd like to use
Wondering if someone can answer something that has stumped me. I have a Timer
Wondering if anyone has gotten the infamous database is locked error from Trac and
Wondering if anybody out there has any success in using the JDEdwards XMLInterop functionality.

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.