【问题标题】:How CakePhp 1.3 handle the data received as HTTP post request?CakePhp 1.3 如何处理作为 HTTP post 请求接收的数据?
【发布时间】:2012-12-30 17:19:14
【问题描述】:

我想创建一个 android 应用程序,它将连接到 CakePhp 网站并在它们之间传递数据。因此,我创建了一个 HTTP 发布请求,将我的“电子邮件”和“密码”传递给 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);函数将发布请求的内容发送回 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**

【问题讨论】:

    标签: android rest cakephp login http-post


    【解决方案1】:

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

    如果您不确定数据是如何发布的,请在您的登录操作中发回 $_POST 的内容:pr($this-&gt;request-&gt;data)

    例如您的登录操作可能如下所示:

    <?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-&gt;request-&gt;data 替换为 $this-&gt;data 并将 is('post') 替换为 isPost()

    您的问题:

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

    或使用echo json_encode($this-&gt;data); 以获得更有条理的回复

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

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

    【讨论】:

    • 非常感谢Yoggi先生的宝贵意见。
    • 请给我一个示例代码,用于将数据从 cakephp 返回到 android ......??????????????
    • 只返回您的用户名和密码,我建议使用 json 数据,例如echo json_encode($this-&gt;data); 然后 exit; 跳过布局和视图中的所有 html
    • 查看这个问题的答案:stackoverflow.com/questions/9605913/… 了解如何处理 android 中的 json 响应
    • 我在 android 中使用 json 编码,但它显示错误“未定义的变量 $arrReturn in Mebuddie/logins/login”以及 cake php 登录页面中的所有 html 内容。我的 Cakephp 代码有什么错误吗???
    猜你喜欢
    • 2010-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 2014-05-05
    • 2014-07-26
    • 2017-12-24
    相关资源
    最近更新 更多