2013-01-16 38 views
0

我想创建一个连接到CakePhp网站并在它们之间传递数据的Android应用程序。所以我创建了一个HTTP post请求,将我的“电子邮件”和“密码”传递给CakePHP loginsController进行登录验证。我使用下面显示的android代码。CakePhp 1.3如何处理作为HTTP post请求接收到的数据?


List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
nameValuePairs.add(new BasicNameValuePair("email", email)); 
nameValuePairs.add(new BasicNameValuePair("password", password)); 
try 
{ 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost("http://10.0.2.2/Mebuddie/logins/login"); 

    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8)); 
    HttpResponse response = httpclient.execute(httppost); 
    HttpEntity entity = response.getEntity(); 
    InputStream is = entity.getContent(); 
    BufferedReader reader = 
      new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8); 
    StringBuilder sb = new StringBuilder(); 
    String line = ""; 
      while ((line = reader.readLine()) != null) 
         { 
         sb.append(line + "n"); 
         } 
     is.close(); 
     Log.i("response", sb.toString()); 

     } 
       catch (Exception e) 
       { 

        e.printStackTrace(); 
       } 

如果有人能回答我的问题,请帮助我。这是我对所有Cakephp和Android专家的要求。我可以在LoginsController上写什么来处理这个请求?

我写Logins_controller下login()函数部分代码如下所示我的CakePHP,

  public function login() { 

      pr($_POST); 

     if ($this->data && 
       isset($this->data['email']) && isset($this->data['password'])) 
     { 
      $arrUser = $this->Login->find('all',array(
        'conditions'=>array(
         'email'=> $this->data['username'], 
         'password' => $this->Auth->password($this->data['password']), 
        ) 
       ) 
      ); 


      if (count($arrUser) > 0) 
      { 
       $arrReturn['status'] = 'SUCCESS'; 
       $arrReturn['data'] = 
         array('loginSuccess' => 1,'id' => $arrLogin[0]['Login']['id']); 
       //logged in 
      } 
      else { 
       $arrReturn['status'] = 'NOTLOGGEDIN'; 
       $arrReturn['data'] = array('loginSuccess' => 0); 
      } 
       echo json_encode($arrReturn); 
     } 

公关($ _ POST);函数将post requst的内容发送给android应用程序。当我打印在Eclipse IDE 它显示的logcat响应,

    <pre>Array 
      (
       [email] => [email protected] 
       [password] => [email protected] 
      ) 
        </pre> 

随着登录表单所有HTML内容。

**我的问题是,

 1.How can i get back only the email & password without the html content. 

     2. how to return the values in the $arrReturn from the cakephp to my android   application 
please give me an example code for return data from cakephp to android** 

回答

0

$this->request->data['email']$this->request->data['password']应该包含您的数据。

如果您不确定数据的发布方式,请在登录操作中将$ _POST的内容发回:pr($this->request->data)

E.g.您的登录操作可以是这样的:

<?php 
public function login() 
{ 
    if ($this->request->is('post')) 
    { 
     $this->Auth->fields = array(
      'username' => 'email', 
      'password' => 'password' 
     ); 
     if ($this->Auth->login($this->request->data)) 
     { 
      //logged in 
     } 
    } 
} 

的CakePHP的版本1.3 $this->data替换$this->request->dataisPost()取代is('post')

您的问题:

  1. 我怎么能不HTML内容找回只有电子邮件&密码。
echo $this->data['email']; 
echo $this->data['password'] 
exit; 

,或者使用echo json_encode($this->data);为更结构化的响应

  • 如何在$ arrReturn从cakephp的返回值以我的Android应用程序请给我一个示例代码返回 数据从cakephp到android
  • 使用json_encode($arrReturn);。有关如何处理json数据的示例,请参阅How to parse JSON in Android

    +0

    非常感谢Yoggi先生的宝贵意见。 –

    +0

    请给我一个从cakephp返回数据到Android的示例代码............ ?????????????? –

    +0

    要仅返回您的用户名和密码,我会建议使用json数据,例如'echo json_encode($ this-> data);'then'exit;'跳过布局和视图中的所有html – Yoggi