【问题标题】:get method in ios json parsing issueios json解析问题中的get方法
【发布时间】:2014-07-19 05:14:15
【问题描述】:

我只想点击 URL: http://kiascenehai.pk/rest_api/todayEvents/api-key/Of7NU7Jimh665D5G5VwO2eKO69sWv9lf/format/json 和参数是city_id。即:/city_id/1 但是;编译器创建错误

Domain=NSURLErrorDomain Code=-1002

 "unsupported URL"

 or
 error 300;

那么在目标 c 的方法中传递参数的最佳方法是什么?它也会导致错误 Domain=kCFErrorDomainCFNetwork Code=303“操作无法完成。

(kCFErrorDomainCFNetwork error 303

如果有人能尽快回复我,我会很高兴。

【问题讨论】:

  • 这个网址没有问题。你能展示你的代码吗?
  • -(void)requestForEvents_cityId:(NSString*)cityid; { NSString *API_URL = [NSString stringWithFormat:@"kiascenehai.pk/rest_api/todayEvents/api-key/…; NSLog(@"我在这里寻找 URL: %@", API_URL); self.httpWorker = [[HttpWorker alloc] init]; [self.httpWorker setDelegate: [[资源 getResources] getEventsDataParser]];
  • NSMutableString * str = [NSMutableString string]; [str appendFormat:@"/city_id/%@",cityid]; NSData * 数据 = [str dataUsingEncoding:NSUTF8StringEncoding]; [self.httpWorker requestPOSTNetwork:API_URL data:data]; [self.httpWorker requestPOSTNetwork:str data:[str dataUsingEncoding:NSUTF8StringEncoding]]; }

标签: ios json methods get


【解决方案1】:

无法重现您提到的问题,可能问题不是因为您使用的 URL 或参数。

这是处理 GET Web 服务调用和从响应中解析数据的最佳方法之一,这里我使用您的 URL 和参数实现了 Web 调用,

// Server data fetch
- (void)getDataForCityId:(NSInteger)cityId
{
    NSMutableString *urlString = [@"http://kiascenehai.pk/rest_api/todayEvents/api-key/Of7NU7Jimh665D5G5VwO2eKO69sWv9lf/format/json/city_id/" mutableCopy];
    [urlString appendFormat:@"%d", cityId];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10.0];
    [request setHTTPMethod:@"GET"];

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
     {
         if (data)
         {
             id jsonObj = [self parseJSON:data];
         }
     }];
}

// Method parses the JSON Data Received
- (id)parseJSON:(NSData *)data
{
    id jsonData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    return jsonData;
}

从响应中解析出来的jsonObj为

【讨论】:

  • 非常感谢您;Alex Andrews 您的代码确实有效。再次感谢好友
  • @user3807662 我在答案中添加的屏幕截图对您有什么问题吗?我可以删除。
  • 不,这很简单,但我不得不嵌入到我的代码中,因为我从你的代码中得到了一些想法,然后进行字符串连接,它只适用于我的应用程序。
猜你喜欢
  • 2015-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-20
  • 1970-01-01
  • 2019-08-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多