2011-06-14 42 views
3

我想使用JSON和webservice从Web服务器获取数据,然后解析JSON响应字符串。 然后我需要将我从网络服务器获取的数据插入到sqlite数据库中。 我已经做了通过HTTP POST方法发布的数据服务器API数据库,这是正常工作,我的数据是越来越保存到我的服务器database.And的响应,我得到的是这种格式:如何在iPhone中使用JSON从Web服务器获取数据

{"TokenID":"od28Denamu","isError":false,"ErrorMessage":"","Result":[{"UserId":"153","FirstName":"Rocky","LastName":"Yadav","Email":"[email protected]","ProfileImage":null,"ThumbnailImage":null,"DeviceInfoId":"12"}],"ErrorCode":900} 

我需要解析JSON数据,并将其保存在由同一个数据库表作为服务器数据库的我的本地SQLite数据库

这是我的代码:

//this is used to post the data to web server database through HTTP POST method 
    -(void)sendRequest 
    { 
     UIDevice *device = [UIDevice currentDevice]; 
     NSString *udid = [device uniqueIdentifier]; 
     NSString *sysname = [device systemName]; 
     NSString *sysver = [device systemVersion]; 
     NSString *model = [device model]; 
     NSLog(@"idis:%@",[device uniqueIdentifier]); 
     NSLog(@"system nameis :%@",[device systemName]); 
     NSLog(@"System version is:%@",[device systemVersion]); 
     NSLog(@"System model is:%@",[device model]); 
     NSLog(@"device orientation is:%d",[device orientation]); 
     NSString *post = [NSString stringWithFormat:@"Loginkey=%@&Password=%@&DeviceCode=%@&Firmware=%@&IMEI=%@",txtUserName.text,txtPassword.text,model,sysver,udid]; 
     NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
     NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 
     NSLog(@"%@",postLength); 
     NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
      //this is my web server api method which consist of 5 parameters Loginkey,Password,DeviceCode,Firmware,IMEI 
    Loginkey takes Username of user 
    Password takes password of user 
    DeviceCode takes device model 
    Firmware takes device Version 
    IMEI takes uniqueidentifier of device. 
     [request setURL:[NSURL URLWithString:@"http://192.168.0.68:91/JourneyMapperAPI?RequestType=Login"]]; 
     [request setHTTPMethod:@"POST"]; 
     [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
     [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
     [request setHTTPBody:postData]; 

     NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

     if (theConnection) { 
      webData = [[NSMutableData data] retain]; 
      NSLog(@"%@",webData); 
     } 
     else 
     { 

     } 

    } 





    -(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 
    {  
     [connection release]; 
     [webData release]; 

    } 
    //this is used to fetch the data through JSON . 
    -(void)connectionDidFinishLoading:(NSURLConnection *)connection 
    {  
     //NSString *loginStatus = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding]; 




     NSString *loginStatus = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding]; 
     NSLog(@"%@",loginStatus); 
     self.webData = nil; 

     NSArray* latestLoans = [(NSDictionary*)[loginStatus JSONValue] objectForKey:@"Login"]; 
     [loginStatus release]; 

     //get latest loan 
     NSDictionary* loan = [latestLoans objectAtIndex:0]; 

     //fetch the data 
     NSString* fundedAmount = [loan objectForKey:@"Loginkey"]; 
     NSString* loanAmount = [loan objectForKey:@"Password"]; 
     NSLog(@"this is foundedamount:%@",fundedAmount); 
     NSLog(@"this is loanAmount:%@",loanAmount); 

     //float outstandingAmount = [loanAmount floatValue] - [fundedAmount floatValue]; 

     //NSString* name = [loan objectForKey:@"name"]; 
     //NSString* country = [(NSDictionary*)[loan objectForKey:@"location"] objectForKey:@"country"]; 

     //set the text to the label 
     //label.text = [NSString stringWithFormat:@"Latest loan: %@ from %@ needs another $%.2f, please help", 
         //name,country,outstandingAmount 
        // ]; 

     //greeting.text = loginStatus; 
     [loginStatus release];   
     [connection release]; 
     [webData release]; 
    } 

但是当我通过JSON获取数据问题代码无法正常工作,发布到服务器工作正常。

当我获取数据NSLOG(@"this is foundedamount:null")值显示。可能是什么问题。请帮我解决这个问题。

感谢

回答

相关问题