Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Mobile > Mobile Forum > iOS - ช่วยด้วยครับ มีปัญหาการอ่านข้อมูล JSON จากเว็บมาแสดงใน Table View



 

iOS - ช่วยด้วยครับ มีปัญหาการอ่านข้อมูล JSON จากเว็บมาแสดงใน Table View

 



Topic : 105783

Guest




เวลาทำแอพผมได้จำลองฐานข้อมูลในเครื่อง จะใช้ URL ที่เชื่อมต่อเป็น http://localhost/Timelineuser.php
ซึ่งมันก็ดึงข้อมูลมาแสดงใน TableView น่ะครับ แต่เมื่อผมลองเปลี่ยนมาใช้ URL จากที่ได้สมัครใช้ web hosting free เป็น http://waterservice.net78.net/Timelineuser.php ข้อมูลมันไม่ขึ้นแสดงใน TableView อ่ะครับ อยากรู้ว่ามันเกิดจากอะไร
ช่วยหน่อยน่ะครับ


ผมนำมา apply จากลิ้งนี้ครับ

https://www.thaicreate.com/mobile/ios-iphone-nsurlconnection-php-mysql-json.html

อันนี้ตอนรันใช้ URL แบบจำลองฐานข้อมูลในเครื่อง

Code PHP ที่ใช้เชื่อมต่อฐานข้อมูลครับ

ตอนรันใช้ URL จาก web hosting free

Code PHP ที่ใช้เชื่อมต่อฐานข้อมูลจากเว็บครับ



Tag : Mobile, iOS, Objective-C, Web Service, Web Hosting







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2014-02-13 08:32:59 By : Ael View : 1219 Reply : 8
 

 

No. 1



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

Quote:
http://waterservice.net78.net/Timelineuser.php


ลองเรียกผ่าน Web Browser บน Simulator ดูครับ มันสามารถเชื่อมต่อได้หรือไม่






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-02-13 08:53:52 By : mr.win
 


 

No. 2

Guest


ตอบความคิดเห็นที่ : 1 เขียนโดย : mr.win เมื่อวันที่ 2014-02-13 08:53:52
รายละเอียดของการตอบ ::
...ลองแล้วครับก็ขึ้นน่ะครับ

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-02-13 11:05:19 By : Ael
 

 

No. 3



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

ถ้าขั้นก็ไม่น่าจะมีปัญหาอะไรน่ะครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-02-13 21:13:53 By : mr.win
 


 

No. 4

Guest


ตอบความคิดเห็นที่ : 3 เขียนโดย : mr.win เมื่อวันที่ 2014-02-13 21:13:53
รายละเอียดของการตอบ ::
ยังไม่ได้ครับ คือถ้ารันบน Web browser จะเป็น String ที่ดึงมาจากฐานข้อมูลแล้ว แบบรูปน่ะครับ

emu

web

ส่วนอันนี้คือรันบน Emu โดย เขียนโค้ดเชื่อมต่อ URL แล้วนำข้อความมา JSON เก็บเป็น สตริงไว้ใน Dict
แล้วให้มันแสดงผลบน Table แต่ผลก็เป็นอย่างในรูปอ่ะครับ ไม่มีข้อมูลแสดง

emu3

อันนี้โค้ดน่ะครับ
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
 


 

No. 5



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

การ Debug ช่วยได้ครับ ลองเรียกค่าง่าย ๆ ดูว่าได้หรือไม่
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-02-14 07:26:28 By : mr.win
 


 

No. 6

Guest


ตอบความคิดเห็นที่ : 5 เขียนโดย : mr.win เมื่อวันที่ 2014-02-14 07:26:28
รายละเอียดของการตอบ ::
ลอง debug เรียกค่าง่ายๆก็ไม่ขึ้นครับ

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-02-14 08:06:18 By : Ael
 


 

No. 7

Guest


debug แล้วค่าที่จะดึงมาแปลงเป็น Null น่ะครับ

null
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-02-14 09:46:16 By : Ael
 


 

No. 8

Guest


โทดนะครับลงรูปผิด debug แล้วค่าที่จะดึงมาแปลงเป็น Null น่ะครับ

debugios
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-02-14 09:49:20 By : Ael
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : iOS - ช่วยด้วยครับ มีปัญหาการอ่านข้อมูล JSON จากเว็บมาแสดงใน Table View
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 05
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่