0

I want to create an android application that will connect to the CakePhp website and pass data between them. So I create a HTTP post request to pass my "email" and "password" to CakePHP loginsController for Login validation I use the android code shown in the below.


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

If anybody can answer my question please help me. It's my request to all Cakephp and Android Experts. What can i write on the LoginsController to handle this request?

I write some code in login() function under Logins_controller in my cakephp shown below,

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

The pr($_POST); function send back the content of the post requst to the android aplication . when i print response in the logcat in the eclipse ide it shows,

                  <pre>Array
             (
                 [email] => imransyd.ahmed@gmail.com
                 [password] => Cpa@2011
             ) 
                    </pre>

Along with All html content in the login form .

**My Question is,

       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**
Sibin Francis
  • 581
  • 2
  • 12
  • 30

1 Answers1

0

$this->request->data['email'] and $this->request->data['password'] should contain your data.

If you are unsure of how the data is posted, send back the content of the $_POST with: pr($this->request->data) inside your login action.

E.g. your login action can look like this:

<?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
        }
    }
}

for CakePHP version 1.3 replace $this->request->data with $this->data and replace is('post') with isPost().

Your questions:

  1. How can i get back only the email & password without the html content.
echo $this->data['email'];
echo $this->data['password']
exit;

or use echo json_encode($this->data); for a more structured response

  1. 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

Use json_encode($arrReturn);. See How to parse JSON in Android for an example how to handle json data.

Community
  • 1
  • 1
Yoggi
  • 572
  • 4
  • 11
  • Thank you very much Mr.Yoggi for your valuable feedback. – Sibin Francis Jan 17 '13 at 07:04
  • please give me an example code for returning data from cakephp to android ............?????????????? – Sibin Francis Jan 17 '13 at 07:41
  • To only return your username and password, I would suggest using json data, e.g. `echo json_encode($this->data);` then `exit;` to skip all html in layout and view – Yoggi Jan 17 '13 at 08:38
  • See the answer to this question: http://stackoverflow.com/questions/9605913/how-to-parse-json-in-android for answer on how to take care of the json response in android – Yoggi Jan 17 '13 at 08:43
  • i use the json encoding in android but it shows the error "undefined variiable $arrReturn in Mebuddie/logins/login"along with all html content in the Login page of cake php. is there any mistake in my Cakephp code??? – Sibin Francis Jan 18 '13 at 05:31
  • Hi, Mr Yoggi, when i use "echo json_encode($this->data)" in my cakephp to return my user name and password ,it shows null in the logcat of eclipse , but insted of using " pr($_POST); " it will return the user name and password that i typed in the android emulator. i cant understand actually whts happening?? – Sibin Francis Jan 21 '13 at 09:18
  • i think the data send from the android is not getting in the cakephp controller. how to solve this? how to get the data send from android to cakephp and vice versa – Sibin Francis Jan 21 '13 at 10:37
  • iam totally confused...please help me – Sibin Francis Jan 21 '13 at 10:37