CakePhp Web サイトに接続し、それらの間でデータを渡す Android アプリケーションを作成したいと考えています。そこで、ログイン検証のために「メール」と「パスワード」を CakePHP loginsController に渡す HTTP ポスト リクエストを作成します。以下に示す 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**