我正在尝试从本地主机获取json数据。我这么做了很多次。但这次它没有获取数据。
Json数据
{
"swimming_pool":"0",
"security":"0",
"lift":"0",
"gym":"0",
"reserved_parking":"0",
"visitor_parking":"0",
"power_backup":"0",
"servant_room":"0",
"tennis_court":"0",
"rainwater_harvesting":"0",
"waste_management":"0",
"club_house":"0",
"desc":"Dkkd",
"city":"City",
"pincode":"Pin Co",
"locality":"locality",
"no_of_beds":"1",
"no_of_baths":"4"
}
客户端代码
{
NSString *selectQuery=[NSString stringWithFormat:@"http://localhost/FilterQuery.php?swimming_pool=%li&&security=%li&&lift=%li&&gym=%li&&visitor_parking=%li&&power_backup=%li&&servant_room=%li&&rainwater_harvesting=%li&&waste_management=%li&&clubhouse=%li&&Posdesc=%@&&no_of_baths=%li&&no_of_beds=%li&&pincode=%li&&locality=%@&&protypedesc=%@",(long)swimpoolb,(long)securityb,(long)liftb,(long)gymb,(long)visparkingb,(long)pbu,(long)servantroom,(long)rainwaterh,(long)wastemanagement,(long)clubHouse,possesion,(long)bathrooms,(long)bedrooms,(long)zipcode,locality,propertyType];
NSString *newInsrStr = [selectQuery stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData *dataaa=[NSData dataWithContentsOfURL:[NSURL URLWithString:newInsrStr]];
NSString *rr=[[NSString alloc]initWithData:dataaa encoding:NSUTF8StringEncoding];
NSLog(@"%@",rr);
jsondataa=[NSJSONSerialization JSONObjectWithData:dataaa options:0 error:nil];
//jsondataa is dictionary
swimmingPool=@"";
swimmingPool=[jsondataa objectForKey:@"swimming_pool"];
security=@"";
security=[jsondataa objectForKey:@"security"];
lift=@"";
lift=[jsondataa objectForKey:@"lift"];
gym=@"";
gym=[jsondataa objectForKey:@"gym"];
reserved_parking=@"";
reserved_parking=[jsondataa objectForKey:@"reserved_parking"];
visitor_parking=@"";
visitor_parking=[jsondataa objectForKey:@"visitor_parking"];
power_backUp=@"";
power_backUp=[jsondataa objectForKey:@"power_backup"];
NSLog(@"%@,%@,%@,%@,%@,%@,%@",swimmingPool,security,lift,gym,reserved_parking,visitor_parking,power_backUp);
}
输出:
2015-06-29 15:20:51.874 NexGV1 [1684:60b]
通知: 未定义的变量:tennis_court in /Applications/XAMPP/xamppfiles/htdocs/FilterQuery.php 在线 21
{ “swimming_pool”: “0”, “安全”: “0”, “升降机”: “0”, “体操”: “0”, “reserved_parking”: “0”, “visitor_parking”: “0”,” power_backup “:” 0" , “servant_room”: “0”, “tennis_court”: “0”, “rainwater_harvesting”: “0”, “waste_management”: “0”, “club_house”: “0”, “降序” : “Dkkd”, “城市”: “城”, “PIN码”:“针 有限公司”, “地方”: “本地”, “no_of_beds”: “1”, “no_of_baths”: “4”} 2015-06-29 15:20:51.875 NexGV1 [1684:60b] (空),(空),(空),(空),(空),(空),(空)
显示空值。为什么呢?
答案 0 :(得分:0)
这可能是评论,但评论的时间太长了。
所以,你的方法是通过url的json请求,它不是理想的这样的东西,它是令人困惑和难以阅读..
我很懒,检查那个很长的网址,所以我只是向您介绍这种方法..
NSString *selectQuery=[NSString stringWithFormat:@"http://localhost/FilterQuery.php?swimming_pool=%li&&security=%li&&lift=%li&&gym=%li&&visitor_parking=%li&&power_backup=%li&&servant_room=%li&&rainwater_harvesting=%li&&waste_management=%li&&clubhouse=%li&&Posdesc=%@&&no_of_baths=%li&&no_of_beds=%li&&pincode=%li&&locality=%@&&protypedesc=%@",(long)swimpoolb,(long)securityb,(long)liftb,(long)gymb,(long)visparkingb,(long)pbu,(long)servantroom,(long)rainwaterh,(long)wastemanagement,(long)clubHouse,possesion,(long)bathrooms,(long)bedrooms,(long)zipcode,locality,propertyType];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:selectQuery]];
您可以将该网址转换为请求,然后使用下面的NSURLConnection
..
__block NSMutableData *fragmentData = [NSMutableData data];
__block id serializedResponse;
[[NSOperationQueue mainQueue] cancelAllOperations];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
[fragmentData appendData:data];
if ([data length] == 0 && error == nil)
{
NSLog(@"No response from server");
}
else if (error != nil && error.code == NSURLErrorTimedOut)
{
NSLog(@"Request time out");
}
else if (error != nil)
{
NSLog(@"Unexpected error occur: %@", error.localizedDescription);
}
else if ([data length] > 0 && error == nil)
{
if ([fragmentData length] == [response expectedContentLength])
{
NSLog(@"Received %f of data from server.", (CGFloat)[response expectedContentLength]);
serializedResponse = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingAllowFragments
error:&error];
NSLog(@"%@", serializedResponse);
// or
NSLog(@"%@", [[NSString alloc] initWithData:fragmentData encoding:NSUTF8StringEncoding]);
}
}
}];
如果您喜欢使用上面NSURLConnection
提出请求的简便方法。
- (NSURLRequest *)convertToRequest:(NSString *)stringURL withDictionary:(NSDictionary *)dictionary
{
NSError *error = nil;
NSData *JSONData = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:&error];
NSURL *url = [NSURL URLWithString:stringURL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody: JSONData];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept-Encoding"];
[request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[JSONData length]] forHTTPHeaderField:@"Content-Length"];
return request;
}
并使用它:
NSDictionary *jsonDictionary = @{
@"swimming_pool": [NSNumber numberWithLong:(long)swimpoolb],
@"security" : [NSNumber numberWithLong:(long)securityb],
.. and so on
};
NSURLRequest *request = [ImplementationClass convertToRequest:YourServerURL withDictionary: jsonDictionary];
并且在服务器中它应该是:
$handle = fopen('php://input','r');
$jsonInput = fgets($handle);
$json_decoded = json_decode($jsonInput,true);
$json_decoded['swimming_pool'];
$json_decoded['security'];
希望这是有益的,有帮助的......