I want to get data from a web server using JSON and webservice, and then parse the JSON response string. then I need to insert the data that I received from the web server into the sqlite database. I sent data to the api server database via the HTTP post method and it works correctly and my data is stored in my server database. And the answer that I get is in this format:
{"TokenID":"od28Denamu","isError":false,"ErrorMessage":"","Result":[{"UserId":"153","FirstName":"Rocky","LastName":"Yadav","Email":"rocky@itg.com","ProfileImage":null,"ThumbnailImage":null,"DeviceInfoId":"12"}],"ErrorCode":900}
I need to parse json data and save it in my local sqlite database, which consists of the same database table as for the server database.
this is my code:
-(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];
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];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *loginStatus = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding];
NSLog(@"%@",loginStatus);
self.webData = nil;
NSArray* latestLoans = [(NSDictionary*)[loginStatus JSONValue] objectForKey:@"Login"];
[loginStatus release];
NSDictionary* loan = [latestLoans objectAtIndex:0];
NSString* fundedAmount = [loan objectForKey:@"Loginkey"];
NSString* loanAmount = [loan objectForKey:@"Password"];
NSLog(@"this is foundedamount:%@",fundedAmount);
NSLog(@"this is loanAmount:%@",loanAmount);
[loginStatus release];
[connection release];
[webData release];
}
But the problem, when I get the data through the JSON code, does not work, the message to the server is working correctly.
NSLOG(@"this is foundedamount:null"), . . , .