Objective C和php交互

时间:2014-01-12 15:51:11

标签: php mysql ios objective-c

嗨,这个waleed我正在创建一个Android应用程序,我想显示从mysql到目标C的数据,因为我已经使用了php以下是我的代码

+(NSDictionary*) callWebservice : (NSString*)url
{
NSDictionary  *dictionary = nil;
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [reachability currentReachabilityStatus];

if (networkStatus == NotReachable) {
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle(angry)"" message:@"Connection not available." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show];
}
else
{

    if (url!=nil && ![url isEqualToString:@""]) {
        NSLog(@"URl : %@",url);
        NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];

        NSError *error = nil;
        NSString *resultData = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        dictionary = [[[SBJSON alloc] init] objectWithString:resultData error:&error];

        if (dictionary == nil)
        {

            NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];

            NSError *error = nil;
            NSString *resultData = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
            dictionary = [[[SBJSON alloc] init] objectWithString:resultData error:&error];

        }
    }
}
return dictionary;

现在我已经在我的视图控制器中调用它了

NSLog(@"data is == %@",[Webservice callWebservice:@"http://geekdev786.com/apilive/UserAccount.php?task=GasCompressor&uid=201&page=5"]);

它给我控制台中的json数组现在我想将它保存在数组中并在表格中显示请帮助

1 个答案:

答案 0 :(得分:-1)

声明为属性NSDictionary(也许dataForTable)。并假设该表名为tableNeedingData

viewDidLoadviewDidAppear中,您会想要这样的内容......

self.dataForTable = [Webservice callWebservice:@"http://oilbaronapp.com/apilive/UserAccount.php?task=GasCompressor&uid=201&page=5"];
// This takes the results of the [Webservice callWebservice:] call and saves it to self.dataForTable

[self.tableNeedingData reloadData];
// This may or may not be necessary depending on when/where you're implementing this
// This just tells the table to refresh itself now that you're certain that self.dataForTable has the data you want to display.

现在,在您的Table DataSource方法中,只需访问self.dataForTable即可获取所有信息。

例如,

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.dataForTable count];
}