-3

我必须从网络服务中检索数据并存储到表格视图中。我有一个框架,但这对我不起作用

这里的问题是使用不允许创建它的创建对象。

谢谢

4

2 回答 2

5

您不需要创建 json.h 文件的对象。您只需要导入 json.h 文件,然后通过[responseString JSONValue].

于 2013-04-04T05:32:09.877 回答
1

检查此代码

@interface videoViewController ()

{ 
  NSMutableData *webData;
  NSURLConnection *connection;
  NSMutableArray *array;
  NSMutableArray *array2;  
}

- (void)viewDidLoad
{

  [super viewDidLoad];
  array=[[NSMutableArray alloc]init];
  array2=[[NSMutableArray alloc]init];
  NSURL *url=[NSURL URLWithString:@"http://localhost/videosphp/videoname2.php"];
  NSURLRequest *request=[NSURLRequest requestWithURL:url];
  connection=[NSURLConnection connectionWithRequest:request delegate:self];
  if(connection)
   {
     webData=[[NSMutableData alloc]init];
   }
}


-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse     *)response
{
  [webData setLength:0];
}


-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
  [webData appendData:data];
}


-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"alert" message:@"Unable to    connect internet. Please check your internet connection" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
  [alert show];
  return;
}


-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
  //use NSDictionary if the data in dictionary
  NSArray *allDataArray=[NSJSONSerialization JSONObjectWithData:webData options:0    error:nil];
  for (NSDictionary *diction in allDataArray) 
  {
    NSString *videoname=[diction objectForKey:@"videoname"];
    [array addObject:videoname]; 
  }
  [[self tableview]reloadData];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
   return 1;   
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return [array count];  
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:        (NSIndexPath    *)indexPath
 {
    static NSString *CellIdentifier=@"Cell";
    UITableViewCell *cell=[tableView    dequeueReusableCellWithIdentifier:CellIdentifier];
    if(!cell)
    {
      cell=[[UITableViewCell  alloc]initWithStyle:UITableViewCellAccessoryDisclosureIndicator     reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text=[array objectAtIndex:indexPath.row];
    return cell;
 }
于 2013-04-04T07:55:36.897 回答