CakePhp 1.3如何处理作为HTTP post请求收到的数据?

时间:2013-01-16 11:38:37

标签: android rest cakephp login http-post

我想创建一个Android应用程序,它将连接到CakePhp网站并在它们之间传递数据。所以我创建了一个HTTP post请求,将我的“email”和“password”传递给CakePHP loginsController进行登录验证我使用下面显示的android代码。


 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上写什么来处理这个请求?

我在下面显示的cakephp中的Logins_controller下的login()函数中编写了一些代码,

         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);
        }

pr($ _ POST);函数将post requst的内容发送回android应用程序。当我在eclipse ide中的logcat中打印响应时 它显示,

                  <pre>Array
             (
                 [email] => imransyd.ahmed@gmail.com
                 [password] => Cpa@2011
             ) 
                    </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**

1 个答案:

答案 0 :(得分: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->request->data替换为$this->data,并将is('post')替换为isPost()

您的问题:

  
      
  1. 我怎样才能收回电子邮件&amp;没有html内容的密码。
  2.   
echo $this->data['email'];
echo $this->data['password']
exit;

或使用echo json_encode($this->data);进行更有条理的回复

  
      
  1. 如何将$ arrReturn中的值从cakephp返回到我的android应用程序请给我一个示例代码返回   从cakephp到android的数据
  2.   

使用json_encode($arrReturn);。有关如何处理json数据的示例,请参阅How to parse JSON in Android