  | 
              
	              
	                
 
 
  
    
        
        iOS - ช่วยด้วยครับ มีปัญหาการอ่านข้อมูล JSON จากเว็บมาแสดงใน Table View     | 
   
  
    |   | 
   
 
 
 
              
  
          
		
     
		
	     
	    
     
               
		
     
		
	     
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ถ้าขั้นก็ไม่น่าจะมีปัญหาอะไรน่ะครับ                        
               
               | 
             
            
              
			  			  
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2014-02-13 21:13:53 | 
                        By :
                            mr.win | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                   ตอบความคิดเห็นที่ : 3 เขียนโดย : mr.win เมื่อวันที่ 2014-02-13 21:13:53 
รายละเอียดของการตอบ ::
   ยังไม่ได้ครับ คือถ้ารันบน Web browser จะเป็น String ที่ดึงมาจากฐานข้อมูลแล้ว แบบรูปน่ะครับ
  
 
ส่วนอันนี้คือรันบน Emu โดย เขียนโค้ดเชื่อมต่อ URL แล้วนำข้อความมา JSON เก็บเป็น สตริงไว้ใน Dict  
แล้วให้มันแสดงผลบน Table แต่ผลก็เป็นอย่างในรูปอ่ะครับ ไม่มีข้อมูลแสดง
  
อันนี้โค้ดน่ะครับ 
 Code
#import "Timeline.h"
#import "CustomCell.h"
#import "DetailTimeline.h"
@interface Timeline ()
{
    NSMutableArray *myObject;
    
    // A dictionary object
    NSDictionary *dict;
    
    // Define keys
    NSString *idpost;
    NSString *detail;
    NSString *parameter;
    NSString *result;
    NSString *imgpost;
    NSString *timestamp;
    
    UIAlertView *loading;
}
@end
@implementation Timeline
@synthesize receivedData,myTableView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
    
    // Define keys
    idpost = @"IDpost";
    detail = @"Detail";
    parameter = @"Parameter";
    result = @"Result";
    imgpost = @"Imgpost";
    timestamp = @"Timestamp";
    
    // Create array to hold dictionaries
    myObject = [[NSMutableArray alloc] init];
    
    NSURLRequest *theRequest =
    [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://waterservice.net78.net/Timelineuser.php"]
                     cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                 timeoutInterval:10.0];
    
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    
    
    // Loading...
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    
    loading = [[UIAlertView alloc] initWithTitle:@"" message:@"กรุณารอ" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
    UIActivityIndicatorView *progress= [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(125, 50, 30, 30)];
    progress.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
    [loading addSubview:progress];
    [progress startAnimating];
    [progress release];
    [loading show];
    
    if (theConnection) {
        self.receivedData = [[NSMutableData data] retain];
    } else {
        UIAlertView *connectFailMessage = [[UIAlertView alloc] initWithTitle:@"NSURLConnection " message:@"Failed in viewDidLoad"  delegate: self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
        [connectFailMessage show];
        [connectFailMessage release];
    }
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    sleep(4);
    [receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    
    [connection release];
    [receivedData release];
    
    // inform the user
    UIAlertView *didFailWithErrorMessage = [[UIAlertView alloc] initWithTitle: @"NSURLConnection " message: @"didFailWithError"  delegate: self cancelButtonTitle: @"Ok" otherButtonTitles: nil];
    [didFailWithErrorMessage show];
    [didFailWithErrorMessage release];
	
    //inform the user
    NSLog(@"Connection failed! Error - %@", [error localizedDescription]);
    
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    [loading dismissWithClickedButtonIndex:0 animated:YES];
    
    if(receivedData)
    {
        id jsonObjects = [NSJSONSerialization JSONObjectWithData:receivedData options:NSJSONReadingMutableContainers error:nil];
        
        // values in foreach loop
        for (NSDictionary *dataDict in jsonObjects) {
            NSString *strIDpost = [dataDict objectForKey:@"IDpost"];
            NSString *strDetail = [dataDict objectForKey:@"Detail"];
            NSString *strParameter = [dataDict objectForKey:@"Parameter"];
            NSString *strResult = [dataDict objectForKey:@"Result"];
            NSString *strImgpost = [dataDict objectForKey:@"Imgpost"];
            NSString *strTimestamp = [dataDict objectForKey:@"Timestamp"];
            NSLog(@"%@", strIDpost);
            NSLog(@"%@", strDetail);
            NSLog(@"%@", strParameter);
            
            dict = [NSDictionary dictionaryWithObjectsAndKeys:
                    strIDpost, idpost,
                    strDetail, detail,
                    strParameter, parameter,
                    strResult, result,
                    strImgpost, imgpost,
                    strTimestamp, timestamp,
                    nil];
            [myObject addObject:dict];
        }
        [myTableView reloadData];
    }
    
    // release the connection, and the data object
    [connection release];
    [receivedData release];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    int nbCount = [myObject count];
    if (nbCount == 0)
        return 1;
    else
        return [myObject count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *CellIdentifier = @"Cell";
    
    CustomCell *Cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!Cell) {
        // Use the default cell style.
        Cell = [[CustomCell alloc] initWithStyle : UITableViewCellStyleDefault
                                       reuseIdentifier : CellIdentifier];
    }
    int nbCount = [myObject count];
    if (nbCount ==0)
        Cell.lblResult.text = @"กำลังโหลด.......................";
    else
    {
        NSDictionary *tmpDict = [myObject objectAtIndex:indexPath.row];
        
        NSURL *url = [NSURL URLWithString:[tmpDict objectForKey:imgpost]];
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *img = [[UIImage alloc] initWithData:data];
        
        Cell.imgTimeline.image = img;
        Cell.lblDetail.text =  [NSString stringWithFormat:@"%@",[tmpDict objectForKey:detail]];
        Cell.lblParameter.text = [NSString stringWithFormat:@"%@",[tmpDict objectForKey:parameter]];
        Cell.lblResult.text =  [NSString stringWithFormat:@"%@",[tmpDict objectForKey:result]];
        Cell.lblTimestamp.text = [NSString stringWithFormat:@"%@",[tmpDict objectForKey:timestamp]];
    }
    return Cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    
    if ([[segue identifier] isEqualToString:@"showDetail"]) {
        
        NSIndexPath *indexPath = [self.myTableView indexPathForSelectedRow];
        
        NSDictionary *tmpDict = [myObject objectAtIndex:indexPath.row];
        
        [[segue destinationViewController] setImgItem:[tmpDict objectForKey:imgpost]];
        [[segue destinationViewController] setNameItem:[tmpDict objectForKey:detail]];
        [[segue destinationViewController] setResultItem:[tmpDict objectForKey:result]];
        [[segue destinationViewController] setMeterItem:[tmpDict objectForKey:parameter]];
        [[segue destinationViewController] setTimeItem:[tmpDict objectForKey:timestamp]];
    }
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)dealloc {
    [myTableView release];
    [super dealloc];
}
@end
ผมไม่เข้าใจว่าเป็นเพราะอะไร  หรือว่าเราต้องไป Run ในมือถือไอโฟน แต่ถ้าใช้ URL ดึงข้อมูลจากแบบจำลองในเครื่องของผม 
มันแสดงผลน่ะครับ วานผู้รู้ช่วยไขปัญหาที 
                         
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2014-02-13 22:26:29 | 
                        By :
                            Ael | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 การ Debug ช่วยได้ครับ ลองเรียกค่าง่าย ๆ ดูว่าได้หรือไม่                        
               
               | 
             
            
              
			  			  
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2014-02-14 07:26:28 | 
                        By :
                            mr.win | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	     
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 debug แล้วค่าที่จะดึงมาแปลงเป็น  Null น่ะครับ  
 
                         
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2014-02-14 09:46:16 | 
                        By :
                            Ael | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 โทดนะครับลงรูปผิด debug แล้วค่าที่จะดึงมาแปลงเป็น Null น่ะครับ  
 
                         
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2014-02-14 09:49:20 | 
                        By :
                            Ael | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
      		  
	
     | 
   
 
                 |