JSON 数据不是来自本地主机

JSON data is not coming from local host

我正在尝试从本地主机获取 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]
Notice: Undefined variable: tennis_court in /Applications/XAMPP/xamppfiles/htdocs/FilterQuery.php on line 21
{"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"} 2015-06-29 15:20:51.875 NexGV1[1684:60b] (null),(null),(null),(null),(null),(null),(null)

显示空值。为什么?

这应该是评论,但评论太长了..

因此,您的方法是 json 通过 url 请求,它不是像这样的东西的理想选择,它令人困惑且难以阅读..

我懒得查那么长url,所以我就介绍一下这种方法..

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]];

您可以将 url 转换为请求,然后使用下面的 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'];

希望这对您有所帮助...