【问题标题】:PHP to JSON to iPhone [duplicate]PHP到JSON到iPhone [重复]
【发布时间】:2011-07-14 12:23:58
【问题描述】:

可能重复:
Cannot Get ASIHTTPRequest callback delegate to trigger

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

[{"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
}

从未调用过代表...谁能解释一下?

【问题讨论】:

    标签: php iphone objective-c xcode asihttprequest


    【解决方案1】:
    - (void)getQuestions:(NSString*)sectionId 
    

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

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

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

      • 我必须更改它们,因为我的实际 php 在返回数据之前需要一个发布的变量 - get_questionstest.php 删除 _POST 变量并使其独立运行...
      • 但是我检查了这个链接:dev.speechlink.co.uk/David/get_questions.php 它没有给出任何回应。您也可以在浏览器中查看。
      猜你喜欢
      • 1970-01-01
      • 2013-02-09
      • 2019-05-10
      • 1970-01-01
      • 2021-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多