2013-04-24 60 views
-1

我一直在下面的教程(http://www.raywenderlich.com/5492/working-with-json-in-ios-5),由此我得到的代碼來處理JSON網絡服務(這裏是我的web服務:http://bit.ly/Y5430d的iOS Web服務無法正常工作

所以,我改變了我的網址我的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 
+1

你聲明這個'NSError *錯誤;'但你從來沒有真正檢查錯誤。 – sosborn 2013-04-24 21:21:33

回答

0

什麼毛病我的web服務?

要說明的簡單方法是使用除您的應用程序以外的東西來訪問它。命令行程序curl是檢查Web服務調用結果的好工具。即使Safari可以在這裏幫助,尤其是在啓用Web檢查器(首選項 - >高級 - >顯示菜單欄中的顯示菜單)的情況下。真的,任何可以讓你看到查詢結果的東西都可以工作。

檢查您的服務的另一種方法是使用Charles的Web代理工具。您可以將您的設備設置爲使用Charles作爲HTTP代理,然後Charles將能夠向您顯示應用和服務器之間的所有流量。

+1

謝謝,這幫了很大的忙! – Bushell 2013-04-25 18:31:24