在ios json解析问题中获取方法 [英] get method in ios json parsing issue
问题描述
我只想点击网址:
http:/ /kiascenehai.pk/rest_api/todayEvents/api-key/Of7NU7Jimh665D5G5VwO2eKO69sWv9lf/format/json
且参数为city_id.ie: / city_id / 1
但;编译器创建错误
域名= NSURLErrorDomain代码= -1002
不支持的URL
或
错误300;
那么在目标c中传递参数的最好方法是什么呢?它也会导致错误域= kCFErrorDomainCFNetwork代码= 303
操作无法完成。
(kCFErrorDomainCFNetwork错误303
如果有人能够快速回复我,可能。
无法重现您提到的问题,可能问题不会因为您使用的网址或参数。
这是处理GET Web服务调用和解析响应数据的最佳方法之一,这里我使用URL和params实现了Web调用, p>
//服务器数据获取
- (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];
}
}];
}
//方法解析接收到的JSON数据
- (id)parseJSON:(NSData *)data
{
id jsonData = [NSJSONSerialization JSONObjectWithData:数据选项:NSJSONReadingMutableLeaves错误:无];
返回jsonData;
}
解析响应的jsonObj为
I just want to hit URL :
http://kiascenehai.pk/rest_api/todayEvents/api-key/Of7NU7Jimh665D5G5VwO2eKO69sWv9lf/format/json
and parameter is city_id.i.e: /city_id/1
but; compiler creates Error
Domain=NSURLErrorDomain Code=-1002
"unsupported URL"
or
error 300;
so what shall be best way to pass arguments in a method in objective c???it also causes Error Domain=kCFErrorDomainCFNetwork Code=303
"The operation couldn’t be completed.
(kCFErrorDomainCFNetwork error 303
It will be pleasure for me if any one can reply me fast as possible.
Unable to reproduce issue you have mentioned, Probably the issue 'll be not because of the URL or parameters you used.
This is one of the best way to handle GET web service call and parsing data from the response, here i implemented the web call with your URL and params,
// 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;
}
The jsonObj parsed form the response is as
这篇关于在ios json解析问题中获取方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!