我想创建一个 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);
}
公关($_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**