2012-09-04 16 views
-1

我無法將我的值發送到我的php文件。 我的應用程序暫停,因爲在發送線程連接iOs值到PHP腳本 - BAD_EXC_ACCES

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    responseData = [[NSMutableData alloc] init]; 
} 





     NSURL *url = [NSURL URLWithString:@"http://www.yoursite.nl/locatie.php"]; 

    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url 
                   cachePolicy:NSURLRequestReloadIgnoringCacheData 
                  timeoutInterval:60]; 

    [theRequest setHTTPMethod:@"POST"]; 
    [theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 

    NSString *postData = [NSString stringWithFormat:@"longitude=%@&latitude=%@&stringFromDate=%@", longitude, latitude, stringFromDate]; 

    NSString *length = [NSString stringWithFormat:@"%d", [postData length]]; 
    [theRequest setValue:length forHTTPHeaderField:@"Content-Length"]; 

    [theRequest setHTTPBody:[postData dataUsingEncoding:NSASCIIStringEncoding]]; 




    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
     responseData = [[NSMutableData alloc] init]; 
    } 

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
     [responseData appendData:data]; 
    } 

    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
     [responseData release]; 
     [connection release]; 
     [textView setString:@"Unable to fetch data"]; 
    } 

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection 
    { 
     NSLog(@"Succeeded! Received %d bytes of data",[responseData 
                 length]); 
     NSString *txt = [[[NSString alloc] initWithData:responseData encoding: NSASCIIStringEncoding] autorelease]; 

    } 

    } 

我的PHP文件看起來像這樣:

<?php 

$id = $_POST['id']; 
$longitude = $_POST['longitude']; 
    $latitude = $_POST['latitude']; 
    $timestamp = $_POST['stringFromDate']; 

    $con = mysql_connect('db.server.nl', 'user', 'pass'); 
    if (!$con) 
    { 
die('Could not connect: ' . mysql_error()); 
} 

    mysql_select_db("db", $con); 

    mysql_query("INSERT INTO locatie (id, longitude, latitude, timestamp) 
    VALUES ('06', 'longitude','latitude','timestamp')"); 

mysql_close($con); 
    ?> 
+0

你可以發佈你實際創建併發送連接的代碼,並指出代碼崩潰的代碼? –

+0

我認爲這是創建連接的部分。我遵循這個指南。 http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html。在這個話題的頂部,我告訴你它崩潰的地方。 –

+0

JSON或XML? PHP腳本沒有返回任何消息,HTTP響應代碼? – nuthan

回答

0

您可以添加以下代碼....

NSString *post = [[NSString alloc] initWithFormat:@"log=%@&pwd=%@",strUserName,strPassword]; 
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
NSString *postLength = [NSString stringWithFormat:@"",[postData length]]; 
NSURL *url = [NSURL URLWithString:@"http://ideaact.neonexusdemo.com/wp-content/themes/idea/andr.php"]; 
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url]; 
[theRequest setHTTPMethod:@"POST"]; 
[theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[theRequest setHTTPBody:postData]; 
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 
if (theConnection) { 
    webData = [[NSMutableData data] retain]; 
} 
+0

完全沒有幫助 –

+0

你檢查我的編輯代碼,它是成功的工作......試試這個......我希望它是幫助你..謝謝 –