0

I am sending reguest to webserver in this function:

-(void)getData
{    
    NSURL *url = [NSURL URLWithString:@"http://10.10.10.10/application.php"]; //some address
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    request.delegate = self;
    [request setPostValue:@"6577098" forKey:@"serial_number"];
    [request startAsynchronous];
}

Then on server part in application.php :

<?php
    //$serial_number from $_POST["serial_number"];
    $serial_number = $this->getString('serial_number', 64); 

    $stmt = $this->pdo->prepare('SELECT S.DateTime, SN.Name FROM States S LEFT JOIN StateNames SN on SN.ID=S.State_ID WHERE S.SerialNumber = ?');

    $stmt->execute(array($serial_number));
    $states = $stmt->fetchAll(PDO::FETCH_ASSOC);
    $result = array();
    foreach ($states as $state)
    {               
        $result[] = $state;            
    }

    sendResponse(200, json_encode($result));
?>

This part works when I try it from browser, it works and I get this:

[{"DateTime":"2013-05-15 10:22:11","Name":"No water"},{"DateTime":"2013-05-13 14:55:31","Name":"Water"}]

Then back to process it in Xcode:

-(void)requestFinished:(ASIHTTPRequest *)request
{
    if (request.responseStatusCode == 200)
    {
       NSString *responseString = [request responseString];
       NSLog(@"%@",responseString);  //if I try this nothing happened
    }
}

I think I have to do something with JSON. I thing I have bad response on my request. But I have responceStatusCode 200. Can anyone help me? I didn't find what's wrong. Thanks

Miras
  • 7
  • 1
  • 8
  • are you sure your server code responds to POST request as well as to GET? – i-- May 15 '13 at 13:30
  • Yes I am sure. If not, it can't return responceStatusCode 200. I try to change or delete that POST value serial_number and if it is wrong it return status code 400. That should be correct. – Miras May 15 '13 at 19:28
  • from your post and comments I still can't understand if it is a server/php issue or the app/objective-c one. can you get a content of any page from server? – i-- May 15 '13 at 20:50
  • Well I need to get data from mysql on server, I use php on it. And I want to get it by ASIHTTPRequest to that server with parameter serial_number and result of that sql select should be response of that request. Just like here http://www.raywenderlich.com/2965/how-to-write-an-ios-app-that-uses-a-web-service but it doesn't work. :( – Miras May 15 '13 at 21:25
  • Just put this code at the top of your php file: `ini_set('display_errors', 1); ini_set('error_reporting', E_ALL);` and you should be able to see the error(s) details, if you have them on the server – i-- May 16 '13 at 13:44
  • Ha, it seem problem should be on server, if I try this on my localhost, response string is here. Do anyone know which php setting should be set? – Miras 6 hours ago – Miras May 16 '13 at 13:58
  • display_errors and error_reporting is set, but nothing helps. It seems to be correct. No errors I thing something must be on server settings, php or apache. Don't you know which module or settings have to be set for this? – Miras May 16 '13 at 14:00

1 Answers1

0

I would need specifics to tell you what server settings you need to adjust. My next best advice - debug line by line. For example in php file, do:

$serial_number = $this->getString('serial_number', 64); //there I get serial_number from $_POST["serial_number"];
echo $serial_number; die;
// ...

at this point if you run your app again, you should see the serial # printed in -(void)requestFinished:(ASIHTTPRequest *)request, if nothing breaks up to here. If you do see it, remove echo and die, move on

//...
$stmt = $this->pdo->prepare('SELECT S.DateTime, SN.Name FROM States S LEFT JOIN StateNames SN on SN.ID=S.State_ID WHERE S.SerialNumber = ?');

$stmt->execute(array($serial_number));
$states = $stmt->fetchAll(PDO::FETCH_ASSOC);
print_r($states); die;
// ...

if no responce there, this might mean that you don't have PDO driver installed with your php.

If the next line sendResponse(200, json_encode($result)); breaks it, then my guess is that your php installation lacks json module.

Community
  • 1
  • 1
i--
  • 4,349
  • 2
  • 27
  • 35
  • I thing something must be on function sendResponse, or not in function but some server setting, something about header or echo $body. Because if I try it by browser, I see that result from database and data printed on screen. But if I try it from Xcode, I didn't see it. But if I try this on localhost, where should be another settings of php and apache, but I don't know what I have to change, there is to many options and modules etc. – Miras May 16 '13 at 17:39
  • this is list of extensions of php on my server: bz2, calendar, Core, ctype, curl, date, ereg, exif, fileinfo, filter, ftp, gettext, hash, iconv, json, libxml, mbstring, mhash, mysql, mysqli, mysqlnd, openssl, pcntl, pcre, PDO, pdo_mysql, pdo_sqlite, Phar, readline, Reflection, session, sockets, SPL, sqlite3, standard, tokenizer, zip, zlib – Miras May 17 '13 at 06:01
  • and this is extensions what I have on localhost and don't have on server: bcmath, dba, dom, gd, ldap, odbc, pdo_pgsql, pgsql, posix, shmop, SimpleXML, snmp, soap, SQLite, sysvmsg, sysvsem, sysvshm, tidy, wddx, xml, xmlreader, xmlrpc, xmlwriter, xsl – Miras May 17 '13 at 06:09
  • and this is list of apache modules which is on localhost and not on server: mod_file_cache, mod_mem_cache, mod_bucketeer, mod_echo, mod_example, mod_case_filter, mod_case_filter_in, mod_reqtimeout, mod_charset_lite, mod_proxy_scgi, mod_imagemap – Miras May 17 '13 at 10:13
  • I am dry at this point - nothing else to suggest, then. Have you tried debugging line by line like I said to find exact place where the script breaks? – i-- May 17 '13 at 13:07
  • well, I was trying adding apache modules nothing happened. but in one moment it start works. I don't know what helps. But it doesn't matter. I guess it was some module missing. Thank you so much for helping me and for moral support. :) – Miras May 17 '13 at 14:01