-1

我一直在关注一个教程(http://www.raywenderlich.com/5492/working-with-json-in-ios-5),我得到了处理 json web 服务的代码(这是我的 web 服务:http ://bit.ly/Y5430d )

所以,我已经将我的 URL 更改为我的 web 服务和 objectforkey 来代表我的(我没有更改任何其他变量)。但是,未格式化的 json 没有通过。

我的网络服务有问题吗?

#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) //1
#define kLatestKivaLoansURL [NSURL URLWithString: @"http://www.bushell.info/getResults.php"] //2

#import "ViewController.h"

@interface NSDictionary(JSONCategories)
+(NSDictionary*)dictionaryWithContentsOfJSONURLString:(NSString*)urlAddress;
-(NSData*)toJSON;
@end

@implementation NSDictionary(JSONCategories)
+(NSDictionary*)dictionaryWithContentsOfJSONURLString:(NSString*)urlAddress
{
    NSData* data = [NSData dataWithContentsOfURL: [NSURL URLWithString: urlAddress] ];
    __autoreleasing NSError* error = nil;
    id result = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    if (error != nil) return nil;
    return result;
}

-(NSData*)toJSON
{
    NSError* error = nil;
    id result = [NSJSONSerialization dataWithJSONObject:self options:kNilOptions error:&error];
    if (error != nil) return nil;
    return result;    
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    dispatch_async(kBgQueue, ^{
        NSData* data = [NSData dataWithContentsOfURL: kLatestKivaLoansURL];
        [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
    });
}

- (void)fetchedData:(NSData *)responseData {
    //parse out the json data
    NSError* error;
    NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData //1
                                                         options:kNilOptions 
                                                           error:&error];
    NSArray* latestLoans = [json objectForKey:@"Latest lotto"]; //2

    NSLog(@"Latest lotto: %@", latestLoans); //3

    // 1) Get the latest loan
    NSDictionary* loan = [latestLoans objectAtIndex:0];

    // 2) Get the funded amount and loan amount
    NSNumber* fundedAmount = [loan objectForKey:@"funded_amount"];
    NSNumber* loanAmount = [loan objectForKey:@"loan_amount"];
    float outstandingAmount = [loanAmount floatValue] - [fundedAmount floatValue];

    // 3) Set the label appropriately
    humanReadble.text = [NSString stringWithFormat:@"Latest loan: %@ from %@ needs another $%.2f to pursue their entrepreneural dream",
                         [loan objectForKey:@"name"],
                         [(NSDictionary*)[loan objectForKey:@"location"] objectForKey:@"country"],
                         outstandingAmount
                         ];

    //build an info object and convert to json
    NSDictionary* info = [NSDictionary dictionaryWithObjectsAndKeys:
                          [loan objectForKey:@"name"], @"who",
                          [(NSDictionary*)[loan objectForKey:@"location"] objectForKey:@"country"], @"where",
                          [NSNumber numberWithFloat: outstandingAmount], @"what",
                          nil];

    //convert object to data
    NSData* jsonData = [NSJSONSerialization dataWithJSONObject:info 
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];

    //print out the data contents
    jsonSummary.text = [[NSString alloc] initWithData:jsonData
                                             encoding:NSUTF8StringEncoding];

}

@end
4

1 回答 1

0

我的网络服务有问题吗?

简单的判断方法是使用您的应用程序以外的其他东西访问它。命令行程序curl是检查 Web 服务调用结果的好工具。甚至 Safari 也可以在这里提供帮助,特别是如果您启用 Web 检查器(首选项->高级->在菜单栏中显示开发菜单)。真的,任何能让你看到查询结果的东西都应该有效。

检查您的服务的另一种方法是使用像 Charles 这样的网络代理工具。您可以将您的设备设置为使用 Charles 作为 HTTP 代理,然后 Charles 将能够向您显示您的应用程序和服务器之间的所有流量。

于 2013-04-24T23:25:04.173 回答