我想创建一个连接到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**
非常感谢Yoggi先生的宝贵意见。 –
请给我一个从cakephp返回数据到Android的示例代码............ ?????????????? –
要仅返回您的用户名和密码,我会建议使用json数据,例如'echo json_encode($ this-> data);'then'exit;'跳过布局和视图中的所有html – Yoggi