Php Response Objective-c ASIHTTPRequest

时间:2014-06-12 15:26:12

标签: php request asihttprequest

我想通过POST发送登录名和密码,在DB中检查并返回相关名称(如果有的话)。

要做到这一点,我有以下php:

<?php

class RedeemAPI {

    private $db;

    // Constructor - open DB connection
    function __construct() {
        $this->db = new mysqli('xxx', 'xxx', 'Xxx', 'Xxx');
        $this->db->autocommit(FALSE);
    }

    // Destructor - close DB connection
    function __destruct() {
        $this->db->close();
    }

    // Main method to redeem a code
    function redeem() {

      // Check for required parameters
        if (isset($_POST['user']) && isset($_POST['password'])) {

            // Put parameters into local variables
            $user = $_POST["user"];
            $password = $_POST["password"];

            // Look up code in database

            $stmt = $this->db->prepare('SELECT nombreCliente FROM Clientes WHERE user=? AND password=?');            
        $stmt->bind_param("ss", $user, $password);
            $stmt->execute();
            $stmt->bind_result($nombreCliente);
            while ($stmt->fetch()) {
                break;
            }
            $stmt->close();

            // Return unlock code, encoded with JSON
            $result = array("nombreCliente" => $nombreCliente);
            sendResponse(200, json_encode($result));
            return true;
        }
        sendResponse(400, 'Invalid request');
        return false;

    }

}

// This is the first thing that gets called when this page is loaded
// Creates a new instance of the RedeemAPI class and calls the redeem method
$api = new RedeemAPI;
$api->redeem();

?>

在Xcode中我有以下方法:

- (IBAction)login:(id)sender {

    NSLog(@"Want to redeem: %@ y %@", self.userText.text, self.passwordText.text);

    NSString *user = self.userText.text;
    NSString *password = self.passwordText.text;
    NSURL *url = [NSURL URLWithString:@"http://192.168.200.2/php/login.php"];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    //NSURLRequest *request   = [NSURLRequest requestWithURL:url];

    //[request setRequestMethod:@"POST"];
    [request setPostValue:user forKey:@"user"];
    [request setPostValue:password forKey:@"password"];
    [request setDelegate:self];
    [request startAsynchronous];

    // first, check to see if there was an error with sendAsynchronousRequest

    NSString *responseString = [request responseString];
    NSDictionary *responseDict = [responseString JSONValue];
    NSString *idCliente = [responseDict objectForKey:@"nombreCliente"];

    NSLog(@"ResponseStatus = %d",request.responseStatusCode);
    NSLog(@"ResponseStatusString = %@",request.responseStatusMessage);
    NSLog(@"ResponseString = %@",responseString);
    NSLog(@"responseDict = %@", responseDict);
    NSLog(@"idCliente = %@",idCliente);

但是,所有NSLog都返回(null)。

与数据库的连接使得它很好,因为我检查了POST并且没有出现故障

我一直在寻找大量信息,许多人建议这样做,但找不到可能是什么问题。

任何人都知道会发生什么?

非常感谢大家!

1 个答案:

答案 0 :(得分:0)

请使用这个可能对您有用

-(void) list_my_product_Called
 {

   ASIFormDataRequest *request1 ;

   request1 = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",STR_URL_login]]];

  [request1 setPostValue:appDel.UserId  forKey:@"user"];
  [request1 setPostValue:appDel.UserToken  forKey:@"password"];

  request1.timeOutSeconds = 140;

  [request1 setDelegate:self];

  // NSLog(@"request body : %@", [[NSString alloc] initWithData:request1.postBody encoding:NSUTF8StringEncoding]);

  [request1 setDidFinishSelector:@selector(requestFinished_login:)];
  [request1 setDidFailSelector:@selector(requestFailed_login:)];
  [request1 startSynchronous]; 
}


 - (void)requestFinished_login:(ASIHTTPRequest *)request {

    SBJsonParser *json = [[SBJsonParser alloc] init];
    NSDictionary* oDict = [json objectWithString:[request responseString]];


    NSLog(@"Signup Response oDict  = %@",oDict);

    if ([[oDict valueForKey:@"status_code"] integerValue] == 1)
    {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success" message:[oDict valueForKey:@"message"]  delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
            alert.tag = 1;
            [alert show];



    return;

   }
   else
   {
       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Fejl" message:[oDict valueForKey:@"msg"]  delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
      [alert show];
      return;
   }
 }

 - (void)requestFailed_login:(ASIHTTPRequest *)request
 {
     NSLog(@"Request Failed %@",[request responseString]);


       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Server        Error" message:STR_ERR_UNKNOWN  delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
    [alert show];
    return;

}