Requesting data from Web Server on iPhone creates too much of a delay
In my iPhone app, I need to access the webserver to login into my system. The client requires that the database be on server only. So I have made an admin panel in ASP.NET and MySQL database where in I store the login information.
Now when I use the method given below in the code to fetch the data from server then it takes too much of time (i.e. More than a minute.) to respond.
In the case where I have fetched a particular data from server before, then that value is fetched fast when I try to fetch it the next time. But for new data it again takes lots of time or rather times out.
Sometimes the request even times out.
What should I do to decrease the delay and speed up the whole login process?
Is there a better way to do this?
Info regarding Code below:
firstname.text is my Username.
lastname.text is my Password.
label.text is a temporary label that I use to store the password obtained from server.
allUsers is the array where the response obtained from server is stored.
Also I am using JSON to parse the data between webserver and iPhone.
I use a plist to store my url. Plist name is url.plist.
Code is as Below:
SBJSON *json = [SBJSON new];
json.humanReadable = YES;
NSString *service = @"/getUserInfo";
//NSString *requestString = [NSString stringWithFormat:@"{\"method\":\"%@\"}", service];
NSString *requestString = [NSString stringWithFormat:@"{\"firstname\":\"%@\"}",firstName.text,nil];
NSLog(@"Request String: %@", requestString);
NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]];
NSString *fileLoc = [[NSBundle mainBundle] pathForResource:@"url" ofType:@"plist" ];
NSDictionary *fileContents = [[NSDictionary alloc] initWithContentsOfFile:fileLoc];
NSString *urlLoc = [fileContents objectForKey:@"baseURL"];
urlLoc = [urlLoc stringByAppendingString:service];
NSLog(@"URL is %@",urlLoc);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:
[NSURL URLWithString: urlLoc]];
NSString *postLength = [NSString stringWithFormat:@"%d", [requestData length]];
[request setHTTPMethod: @"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody: requestData];
//Data returned by WebService
NSError *respError = nil;
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: &respError ];
if (respError) {
NSString *msg = [NSString stringWithFormat:@"Connection failed! Error - %@ %@",
[respError localizedDescription],
[[respError userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Check your network connection"
message:msg delegate:self cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
NSArray *keys = [NSArray arrayWithObjects:@"firstname", @"lastname", nil];
NSArray *objects = [NSArray arrayWithObjects:@"failed to", @"refresh data...", nil];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
allUsers = [[NSArray alloc] initWithObjects:dictionary, nil];
//[self setUserData:allUsers];
//[tblView reloadData];
//[allUsers release];
}
else
{
NSString *responseString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];
NSDictionary *results = [responseString JSONValue];
// Additional steps as the webservice is adding an additional "{d:" so stripping of that
NSString *extractUsers = [results objectForKey:@"d"];
// The actual string that Web services returned, so re-scan the same and convert it as object
NSDictionary *finalResult = [extractUsers JSONValue];
allUsers = [finalResult objectForKey:@"users"];
NSLog(@"Data is : %@",allUsers);
开发者_运维知识库 NSLog(@"Final Value is : %@",[[allUsers objectAtIndex:0] valueForKey:@"lastname"]);
if([allUsers count]>0)
{
label.text = [[allUsers objectAtIndex:0] valueForKey:@"lastname"];
}
else
{
label.text = @"";
}
[responseString release];
[request release];
}
[inProgressIndicator stopAnimating];
[fileContents release];
//Release all the allocated data
[json release];
}
At first, you should check on which side delay is taking place - is it server's lag (maybe, not optimized DB usage or so on), internet-connection issue or another problem. Just trace and check, which line of code is executed for that minute at first.
精彩评论