ใช้บริการเว็บ PHP ในแอพ iPhone

ฉันได้พัฒนาบริการเว็บด้วย PHP มันใช้ฐานข้อมูล MySQL ในนี้ฉันใช้ JSON ฉันต้องการดึงข้อมูลจากบริการเว็บนี้ในแอพ iPhone ของฉัน ฉันใช้ฟังก์ชันซึ่งมีพารามิเตอร์ตัวเดียว ฉันจะใช้สิ่งนี้ใน iPhone ได้อย่างไร

บริการเว็บ:

<?php

echo getdata($_REQUEST['lastupdate']);

function getdata($lastupdatedate){

    $json = '{"foo-bar": 12345}';



    $obj = json_decode($json);

    //print $obj->{'foo-bar'}; // 12345



    $con = mysql_connect("localhost","un","pwd");



    if (!$con)



    {



    die('Could not connect: ' . mysql_error());



    }

    //print_r($con);



    mysql_select_db("roster", $con);







    $query = "select * from rates where LastUpdated = '".$lastupdatedate."' order by LastUpdated limit 1";

    $rs = mysql_query($query) or die($query);

    //print_r($rs);

    while($row=mysql_fetch_assoc($rs)){

    $record[] = $row;

    }



    $data = json_encode($record);



    header('Cache-Control: no-cache, must-revalidate');

    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');

    header('Content-type: application/json');

    return $data;

}

?>

ฉันกำลังขอ URL: http://domain/t1.php?lastupdate=2012-09-01 01:00:00


person Curious_k.shree    schedule 01.10.2012    source แหล่งที่มา


คำตอบ (1)


คุณจะต้องสร้างคำขอสำหรับ http://domain/t1.php?lastupdate=2012-09-01 01:00:00 จากนั้นแยกวิเคราะห์การตอบสนอง JSON ของคุณ

ขึ้นอยู่กับว่าโค้ดของคุณควรทำงานอย่างไร (โหลดหรือขึ้นอยู่กับการกระทำของผู้ใช้) โค้ดของคุณควรทำงานคล้ายกับตัวอย่างนี้จาก raywenderlich.com

ข้อมูลโค้ดพื้นฐาน:

- (void)viewDidLoad
{
    [super viewDidLoad];

    dispatch_async(kBgQueue, ^{
        NSData* data = [NSData dataWithContentsOfURL:yourURLValue];
        [self performSelectorOnMainThread:@selector(fetchedData:) 
          withObject:data waitUntilDone:YES];
    });
}

- (void)fetchedData:(NSData *)responseData {
    //parse out the json data
    NSError* error;
    NSDictionary* json = [NSJSONSerialization 
        JSONObjectWithData:responseData //1

        options:kNilOptions 
        error:&error];

    NSArray* JSONResultValues = [json objectForKey:@"yourKey"]; //2

    NSLog(@"Results: %@", JSONResultValues); //3
}
person propstm    schedule 01.10.2012
comment
ฉันไม่เข้าใจในลิงก์ url ฉันควรผ่านอะไร URL ของฉันคือ:domain/t1.php?lastupdate=2012-09-01 01:00:00 โดยที่ 2012-09-01 01:00:00 เป็นพารามิเตอร์อินพุตของฉัน - person Curious_k.shree; 01.10.2012
comment
NSString *myvalue = @ 2012-09-01 01:00:00; NSURL *yourURLValue = [[การจัดสรร NSURL] initWithString:[NSString stringWIthFormat: @domain/t1.php?lastupdate=%@,myValue]]; // คุณสามารถสร้าง URL ที่แตกต่างกันโดยแต่ละแห่งใช้ค่าพารามิเตอร์ที่เหมาะสม - person propstm; 01.10.2012
comment
ท่านครับ เมื่อผมทำ NSLog(yourURLValue); - person Curious_k.shree; 03.10.2012