2011-08-11 269 views
3

我試圖在我的應用程序中運行一個php腳本,它將檢查用戶的登錄變量與數據庫中的那些變量。下面的代碼是我如何執行該文件。我正在運行該腳本,然後將結果存儲在數組中。 「是」結果意味着用戶的登錄數據是正確的,「否」結果意味着它是不正確的。IPhone IOS:PHP腳本需要花費太多時間來執行?

此代碼有效,但我的問題是時間。我已經設置了定時器,即使我的php文件是空白的,也需要8秒來執行第一個塊。其他的一切,甚至加載下一個頁面的速度要快得多的一個代碼塊中(3秒鐘TOPS)

//first block 
NSString * post = [NSString stringWithFormat:@"userId=%@&password=%@",userId.text,password.text]; 
NSString * hostStr = @"http://domain.com/check_login.php?"; 
hostStr = [hostStr stringByAppendingString:post];  
NSURL *location = [NSURL URLWithString:hostStr]; 
NSDictionary *dictionaryData = [[ NSDictionary alloc ] initWithContentsOfURL:location ]; 

//second block 
NSArray *resultArray = [dictionaryData objectForKey:@"result"]; 

NSArray *loginidArray = [dictionaryData objectForKey:@"loginid"]; 

NSArray *usernameArray = [dictionaryData objectForKey:@"username"]; 

同時,約1開出4次,我得到這個錯誤「SendDelegateMEssage:委託失敗返回等待10秒後,主運行循環模式:_kCFURLConnectionPrivateRunLoopMode「。我一直在試圖查找它,但根據我的代碼,我不知道它的含義。當我得到這個錯誤時,代碼可能需要30-60秒才能執行!

任何方向將不勝感激。

感謝,

阿曼達

編輯,包括PHP,櫃面它有助於知道發生了什麼服務器端的代碼。

<?php 
header('Content-type: text/xml'); 
session_start(); 
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE plist SYSTEM \"file://localhost/System/Library/DTDs/PropertyList.dtd\">\n<plist version=\"1.0\">\n"; 

$username = "user"; 
$password = "pass"; 
$host = "localhost"; 
$database = "database"; 

// Make the connect to MySQL or die 
// and display an error. 
$link = mysql_connect($host, $username, $password); 
if (!$link) { 
die('Could not connect: ' . mysql_error()); 
error_log("CANT CONNECT DB"); 
} 
else 
{ 
error_log("DB CONNECTED"); 
} 


mysql_select_db ($database); 


    $userId  = $_GET['userId']; 
    $password = $_GET['password']; 


    $sql = "SELECT user_login_id,userid from user_login WHERE userId ='". $userId ."' AND password ='".$password."'"; 
    $rs_login = mysql_query($sql, $link) or die (mysql_error()); 

    if(mysql_num_rows($rs_login) ==1) 
    { 

     $row = mysql_fetch_array($rs_login); 
     $user_login_id = $row['user_login_id']; 
     $user_login_name = $row['userid']; 

    // The data that needs to be transferred: 
    $my_data = array(); 

    $my_data["result"]  = array("YES"); 
    $my_data["loginid"]  = array($user_login_id); 
    $my_data["username"]  = array($user_login_name); 

    // Open the dictionary 
    echo "<dict>\n"; 

    // Putting the data into the plist format 
    foreach ($my_data as $key => $array) 
    { 
    // Loop for every value in $my_data 
    echo " <key>$key</key>\n <array>\n"; // Make a key with the key from $my_data and open an array for it's values. 
    foreach ($array as $value) 
    { // Go through every value in the current array from $my_data 
     echo "  <string>$value</string>\n"; // Print the value as a string in the array 
    } 
    echo " </array>\n"; // Close the array 
    } 

    echo "</dict>\n</plist>\n"; // Close the dictionary & plist 

} 
else 
{ 
    $my_data["result"] = array("NO"); 
    $my_data["loginid"] = array("--"); 
    $my_data["username"]  = array("--"); 

    // Open the dictionary 

    echo "<dict>\n"; 

    // Putting the data into the plist format 
    foreach ($my_data as $key => $array) 
    { 
    // Loop for every value in $my_data 
    echo " <key>$key</key>\n <array>\n"; // Make a key with the key from $my_data and open an array for it's values. 
    foreach ($array as $value) 
    { // Go through every value in the current array from $my_data 
     echo "  <string>$value</string>\n"; // Print the value as a string in the array 
    } 
    echo " </array>\n"; // Close the array 
    } 
    echo "</dict>\n</plist>\n"; // Close the dictionary & plist 



} 
mysql_free_result($rs_login); 

?> 


解決方案
這是我跟去了,只需要4秒鐘登錄現在...謝謝你們。

NSHTTPURLResponse * response = nil; 
NSError* error = nil; 

NSString * post = [NSString stringWithFormat:@"userId=%@&password=%@",userId.text,password.text]; 
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
[request setURL: [NSURL URLWithString:@"http://domainname.com/check_login.php?"]]; 

[request setHTTPMethod:@"POST"]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
[request setHTTPBody:postData]; 

NSData *returnData = [NSURLConnection sendSynchronousRequest:request 
              returningResponse:&response 
                 error:&error]; 
NSString *result = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 


NSString *errorDesc = nil; 
NSPropertyListFormat format; 

NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization 
             propertyListFromData:returnData 
             mutabilityOption:NSPropertyListMutableContainersAndLeaves 
             format:&format 
             errorDescription:&errorDesc]; 
if (!temp) { 
    NSLog(@"Error reading plist: %@, format: %d", errorDesc, format); 
} 

NSArray *resultArray = [temp objectForKey:@"result"]; 

NSArray *loginidArray = [temp objectForKey:@"loginid"]; 

NSArray *usernameArray = [temp objectForKey:@"username"]; 
+0

嘗試從桌面Web瀏覽器訪問該文件時,服務器響應需要多長時間? – esqew

+0

一秒鐘......不均勻。就像我說的那樣,訪問這個文件需要很長時間,即使文件是空白的。 – amanda

+1

出於我自己的好奇心,輸出如何格式化,使您可以直接從中形成字典?在我的Web服務中,我通常輸出JSON,然後使用解析器轉換爲字典。並且,如果您發佈PHP也可能有所幫助 –

回答

1
NSString* res = nil; 
NSHTTPURLResponse * response = nil; 
NSError* error = nil; 

NSString * post = [NSString stringWithFormat:@"userId=%@&password=%@",userId.text,password.text]; 
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
[request setURL: [NSURL URLWithString:@"http://domain.com/check_login.php"]]; 

[request setHTTPMethod:@"POST"]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
[request setHTTPBody:postData]; 

NSData *returnData = [NSURLConnection sendSynchronousRequest:request 
             returningResponse:&response 
             error:&error]; 
NSString *result = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 

我其實只是在做一種與上面的代碼同樣的事情,它需要我不到一秒鐘。試一試!

也嘗試通過您的瀏覽器訪問您的check_login.php並查看是否需要很長時間才能加載。如果是這樣的話,那就是你的PHP文件導致了這個問題。如果它立即加載(就像它應該)它是你的Objective-C代碼。

+0

如果你這樣做,記得把'$ _GET'改爲'$ _POST'或' $ _REQUEST' –

+0

@Jules ...按照你的例子,並改變$ _GET爲$ _REQUEST像機器人建議...我如何存儲結果,所以我可以訪問字段?我試過「NSDictionary * dictionaryData = [[NSDictionary alloc] initWithContentsOfFile:result];」 ,但當我嘗試訪問resultArray的索引0時,我得到一個(null)值。 – amanda

+1

@amanda你是什麼意思?你有沒有嘗試我的代碼片段?因爲那只是調用你的網頁,並把結果放在'NSString * result = ...'中。所以請嘗試把結果放在一個'NSLog'中,看看你得到了什麼。如果一切正常,它會顯示你的XML輸出。 – Jules

相關問題