PHP to JSON to iPhone


PHP to JSON to iPhone

可能重复:
无法获取ASIHTTPRequest回调委托以触发

我有一个php文件,它输出以下JSON:

[{"0":"5","questionId":"5","1":"Morning Heart Rate","question":"Morning Heart Rate","2":"5","questionNumber":"5","3":"1","sectionId":"1"},{"0":"4","questionId":"4","1":"Evening Urine Colour","question":"Evening Urine Colour","2":"4","questionNumber":"4","3":"1","sectionId":"1"},{"0":"3","questionId":"3","1":"Evening Bodyweight","question":"Evening Bodyweight","2":"3","questionNumber":"3","3":"1","sectionId":"1"},{"0":"2","questionId":"2","1":"Morning Urine Colour","question":"Morning Urine Colour","2":"2","questionNumber":"2","3":"1","sectionId":"1"},{"0":"1","questionId":"1","1":"Morning Bodyweight","question":"Morning Bodyweight","2":"1","questionNumber":"1","3":"1","sectionId":"1"},{"0":"6","questionId":"6","1":"Time of Month (TOM)","question":"Time of Month (TOM)","2":"6","questionNumber":"6","3":"1","sectionId":"1"}]

这可以在以下链接中看到:

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

我在Objective C中使用JSONKit作为解码JSON的框架。以下方法用于与php通信:

//方法

+(void)getQuestions:(NSString*)sectionId{
    NSString* url = @"http://dev.speechlink.co.uk/David/get_questions.php";
    NSURL *link = [NSURL URLWithString:url];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:link]; 
    [request setPostValue:sectionId forKey:@"section"]; 
    [request setDelegate:self];
    [request setDidFinishSelector:@selector(requestFinished:)];
    [request startAsynchronous];    
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
    //NSString *response = [request responseString];
    NSLog(@"hello"); //never prints
}

从未调用委托。。。有人能解释一下吗?

- (void)getQuestions:(NSString*)sectionId 

将该方法声明为实例方法,而不是类方法。

设置[request setDelegate:self];时,self必须是已分配的对象。

我检查链接http://dev.speechlink.co.uk/David/get_questionstest.php正如你所描述的那样,它给出了回应。但在您的代码中,您使用的链接与上面提供的链接不同。

http://dev.speechlink.co.uk/David/get_questions.php它没有给出任何响应,这可能就是您的代表没有打电话的原因。请将"get_questionstest.php"更改为"get_questionstest.php",它将正常工作。

如果有任何问题,请告诉我。