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

  • SEARCH
  • Home
  • 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 6148725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:19:47+00:00 2026-05-23T19:19:47+00:00

I have an iPhone app which sends a request to a url posting a

  • 0

I have an iPhone app which sends a request to a url posting a variable called submit:

+(NSMutableArray*)getQuestions:(NSString*)section from: (NSString*) url{
    NSMutableArray *questions = [[NSMutableArray alloc] init];
    //connect to database given by url
    //NSError        *error = nil;
    //NSURLResponse  *response = nil;
    NSMutableString* myRequestString = [[NSMutableString string]initWithFormat:@"section=%@", section];
    NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: url]]; 
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
    [request setHTTPMethod: @"POST"];
    //post section 
    [request setHTTPBody: myRequestData];

    //store them in the array;
    return [questions autorelease];
}

My php file:

<?php

//connect to database
function connect() {
  $dbh = mysql_connect ("localhost", "abc1", "12345") or die ('I cannot connect to the database because: ' . mysql_error());
  mysql_select_db("PDS", $dbh); 
  return $dbh;
}

//store posted data
if(isset($_POST['section'])){
  $dbh = connect();
  $section = $_POST['section'];
  $query = mysql_query("SELECT * FROM QUESTIONS WHERE sectionId = $section;") or die("Error: " . mysql_error());;

  $rows = array();
  while($r = mysql_fetch_assoc($query)) {
    $rows[] = $r;
  }
  echo '{"questions":'.json_encode($rows).'}';
  mysql_close();
}
?>

I have built a model class (Question) in objective c which has the exact properties that each row element has in the rows associative array.

My questions are:

1) How can I read the echo’d JSON array elements and their relative attributes in objective C?

2) How can I create an array of Question objects and map each one to an element in the rows array?

3) What do I have to write in my method “+(NSMutableArray*)getQuestions:(NSString*)section from: (NSString*) url” to capture the reply from the php (the echo)?

EDIT:

Here is the output of the php:

http://dev.speechlink.co.uk/David/get_questionstest.php

UPDATE

Changed method to use ASIHTTPRequest – Cannot deserialise JSON string:

//method to 
+(NSDictionary*)getQuestions:(NSString*)sectionId from: (NSString*) url{
    NSDictionary *questions;
    NSURL *link = [NSURL URLWithString:url];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:link];
    [request setPostValue:sectionId forKey:@"section"];
    NSError *error = [request error];
    [request startAsynchronous];

    if (!error) {       
        //NSString *response = [request responseString];
        //store them in the dictionary
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
        NSString *json = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        questions = [json objectFromJSONString];
        NSLog(@"Data: %@", questions); //outputs Data: (null)
        [json release];
        [request release];      
    }else{
        //UIAlertView to warn users there was an error
    }               

    return questions;   
}
  • 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-23T19:19:48+00:00Added an answer on May 23, 2026 at 7:19 pm

    Well, lets go through this one step at a time.

    You can create a NSDictionary from JSON quite easily by using one of several different JSON parsing libraries. I really enjoy using JSONKit. Once you’ve imported JSONKit, into your project, you can do something like this:

    NSString *url = @"http://dev.speechlink.co.uk/David/get_questionstest.php";
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
    NSString *json = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSDictionary *questions = [json objectFromJSONString];
    [json release];
    

    Now you have an array filled with the questions in your example. Now you can loop through this array and fill your data with the data in the array. Now lets be practical. It would be easier if you just had to manage one object instead of four for each question, wouldn’t it? Lets make a class that contains one question each instance.


    Interface:

    @interface Question : NSObject {
        NSString *questionId;
        NSString *question;
        NSString *questionNumber;
        NSString *sectionId;
    }
    @property(copy)NSString *questionID; 
    @property(copy)NSString *question; 
    @property(copy)NSString *questionNumber; 
    @property(copy)NSString *sectionId; 
    
    @end
    

    And implementation:

    @implementation Question
    @synthesize questionId, question, questionNumber, sectionID;
    
    @end
    

    Now that’s just a basic example. Nothing fancy. Now you can loop through the array you had before and create “question” objects that contain each question’s data. For my purposes, suppose you have a NSMutableArray named questionsArray that contain the questions you want to use. We’ll loop through the dictionary and add the questions from the dictionary into the questionsArray array.

    for (NSDictionary *q in questions) {
        /* Create our Question object and populate it */
        Question *question = [[Question alloc]init];
        question.questionId = [q objectForKey:@"questionId"];
        question.question = [q objectForKey:@"question"];
        question.questionNumber = [q objectForKey:@"questionNumber"];
        question.sectionId = [q objectForKey:@"sectionId"];
    
        /* Add it to our question (mutable) array */
        [questionsArray addObject:question];
        [question release];
    }
    

    Tada! Now you have an array filled with Question objects. Any time you want to look at a property on any of the question objects, you can just simply access that property. For example, to grab the first question’s number, you can just do this:

    NSString *q1Number = [questionsArray objectAtIndex:0].questionNumber;
    

    Please note this is all untested, as I don’t have my compiler with me. It should get you started, though. =)


    Edit: You were doing your request completely wrong. Try this:

    +(NSDictionary*)getQuestions:(NSString*)sectionId from: (NSString*) url{
        NSDictionary *questions = nil;
        NSURL *link = [NSURL URLWithString:url];
        ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:link];
        [request setPostValue:sectionId forKey:@"section"];
        NSError *error = [request error];
        [request startSynchronous];
    
        if (!error) { 
            NSData *response = [request responseData];
            NSString *json = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
            questions = [json objectFromJSONString];
            [json release];
        } else{
            //UIAlertView to warn users there was an error
        }               
        [request release];
         return questions;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an iPhone app which hides the status bar when run. After launching
I have an iPhone app which I need to test in an older version
I have an iPhone app which submits user entered data to a SQL database.
I have an iphone app which streams in a website/blogs rss feed. Will each
So I have an iPhone app which should aid the user to find a
I have an iPhone app that hides the status bar. However, my main view
I have an iPhone app that compiles and runs fine in the Simulator on
I have an iphone app where I call these three functions in appDidFinishLaunching: glMatrixMode(GL_PROJECTION);
I have a simple iphone app that's based on the CrashLanding sample app. So
In my iPhone app, I have put a UIBarBUtton of type UIBarButtonSystemItemTrash in my

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.