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**
$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.
Eg 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:
- 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
- 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.