在我的iPhone應用程序中,我需要訪問Web服務器才能登錄到我的系統。客戶端要求數據庫僅在服務器上。所以我在ASP.NET和MySQL數據庫中創建了一個管理面板,用於存儲登錄信息。從iPhone上的Web服務器請求數據會產生太多延遲
現在,當我使用代碼中給出的方法從服務器獲取數據時,它需要花費太多時間(即超過一分鐘)來響應。
在我以前從服務器獲取特定數據的情況下,當我嘗試下次獲取該值時,該值會被快速獲取。但對於新數據而言,它又需要很多時間或者超時。
有時候,請求甚至超時。
我應該怎麼做才能減少延遲並加速整個登錄過程?
有沒有更好的方法來做到這一點?
信息關於下面的代碼:
firstname.text是我的用戶名。
lastname.text是我的密碼。
label.text是一個臨時標籤,我用它來存儲從服務器獲取的密碼。
allUsers是存儲從服務器獲取的響應的數組。
此外,我正在使用JSON來解析Web服務器和iPhone之間的數據。
我使用plist來存儲我的網址。 Plist的名字是url.plist。
守則如下:
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];
}
正如Nickolay所說,問題可能是服務器端。例如,MySQL服務器可能被配置爲按需啓動,這意味着您第一次使用該服務時,必須啓動數據庫服務器。我會首先確定它不是問題的服務。您可以通過確保它也會以查詢鍵作爲參數來響應GET請求,然後簡單地將Web瀏覽器指向它來做到這一點。 – JeremyP 2011-01-12 12:04:36