Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 使用AFJSONRequestOperation_Iphone_Objective C_Ios_Json_Afnetworking - Fatal编程技术网

Iphone 使用AFJSONRequestOperation

Iphone 使用AFJSONRequestOperation,iphone,objective-c,ios,json,afnetworking,Iphone,Objective C,Ios,Json,Afnetworking,我试图从提供JSON的web服务中获取一些数据。但我不知道我的代码出了什么问题。它看起来很简单,但我无法获得任何数据 这是代码: NSURLRequest *request = [NSURLRequest requestWithURL:URL]; AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLReq

我试图从提供JSON的web服务中获取一些数据。但我不知道我的代码出了什么问题。它看起来很简单,但我无法获得任何数据

这是代码:

NSURLRequest *request = [NSURLRequest requestWithURL:URL];

AFJSONRequestOperation *operation = [AFJSONRequestOperation
   JSONRequestOperationWithRequest:request 
   success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
     DumpDic = (NSDictionary*)[JSON valueForKeyPath:@"description"] ;
   } 
   failure:nil
];

[operation start];

AboutTXT = [DumpDic objectForKey:@"description"];
这是你的电话号码

编辑

来自URL的JSON:

{
   "clazz":"AboutList",
   "description":{
      "clazz":"DescriptionContent",
      "description":"ASTRO Holdings Sdn. Bhd. (AHSB) Group operates through two holding companies – ASTRO Overseas Limited (AOL) which owns the portfolio of regional investments and ASTRO Malaysia Holdings Sdn Bhd (AMH / ASTRO) for the Malaysian business, which was privatized in 2010 and is currently owned by Usaha Tegas Sdn Bhd/its affiliates, and Khazanah Nasional Berhad."
   },
   "id":{
      "inc":-1096690569,
      "machine":1178249826,
      "new":false,
      "time":1339660115000,
      "timeSecond":1339660115
   },
   "refKey":"AboutList"
}

它是否成功连接到服务器,是否调用了成功块

填写故障块并记录故障块返回的NSError:

failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
                                                        NSLog(@"%@", [error userInfo]);
                                                    }
我还有一个建议,我建议使用AFNetwork的AFHTTPClient构建NSURLRequest,它有助于处理各种事情,通常会使事情变得更简单。设置基URL,然后为其提供一个附加到该基URL的路径。大概是这样的:

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:address];
[httpClient setParameterEncoding:AFJSONParameterEncoding];
NSMutableURLRequest *jsonRequest = [httpClient requestWithMethod:@"POST"
                                                            path:@"events"
                                                      parameters:dict];
AFJSONRequestOperation *operation =
  [AFJSONRequestOperation JSONRequestOperationWithRequest:request
                                                  success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    DumpDic = [JSON objectFor:@"description"];
    AboutTXT = [DumpDic objectForKey:@"description"];
  }
  failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
    NSLog(@"%@", [error userInfo]);
  }];

[operation start];
另外,我建议您只使用objectForKey,而不是使用valueForKeyPath:

[JSON objectForKey:@"description"];
此外,您不应在此处访问DumpDic:

[operation start];
AboutTXT = [DumpDic objectForKey:@"description"];
这是一个异步调用,因此一旦操作开始,DumpDic很可能在从服务器分配数据之前被访问。因此,您正在访问一个可能还不存在的密钥

这应该在成功或失败块中完成。一旦连接完成并且数据准备好使用,就会调用这些块

所以它看起来应该更像这样:

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:address];
[httpClient setParameterEncoding:AFJSONParameterEncoding];
NSMutableURLRequest *jsonRequest = [httpClient requestWithMethod:@"POST"
                                                            path:@"events"
                                                      parameters:dict];
AFJSONRequestOperation *operation =
  [AFJSONRequestOperation JSONRequestOperationWithRequest:request
                                                  success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    DumpDic = [JSON objectFor:@"description"];
    AboutTXT = [DumpDic objectForKey:@"description"];
  }
  failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
    NSLog(@"%@", [error userInfo]);
  }];

[operation start];