我写了一个从用户那里获取用户名和密码的Android登录应用程序。我想发送用户名&服务器的密码是 cakephp应用程序。这是我的android代码:
httpclient=new DefaultHttpClient();
httppost= new HttpPost("http://10.0.2.2/ar/users/login?name="+name+"&password="+pass);
//Execute HTTP Post Request
response=httpclient.execute(httppost);
final ResponseHandler<String> responseHandler = new BasicResponseHandler();
result = httpclient.execute(httppost, responseHandler);
System.out.println("Response : " + response);
JSONArray jArray;
jArray = new JSONArray(result);
JSONObject j =new JSONObject();
j = jArray.getJSONObject(0);
JSONObject json = j.getJSONObject("User");
text = json.getString("last_name");
我只在布局中写下这一行:
<?php echo $content_for_layout ?>
查看代码:
<?php echo json_encode($user); ?>
控制器:
function login() {
if(isset($this->params['url']['name']))
$data = $this -> User -> findByuser_name($this->params['url']['name']);
if ($data && $data['User']['password'] == ($this->params['url']['password'])) {
$this -> set('user', $data);
} else {
$this -> set('error', true);
}
}
这段代码很好地通过在浏览器中输入以下url但在Android应用程序中不起作用! “?本地主机/ AR /用户/登录名= m_sepehri&安培;密码= 111” 有人可以帮帮我吗?
答案 0 :(得分:1)
您正在使用httppost,但会附加您的数据名称并传递到网址本身。使用AsyncTask和List,如下所示:
private class MyAsyncTask extends AsyncTask<String, Integer, Double> {
@Override
protected Double doInBackground(String... params) {
// TODO Auto-generated method stub
postData(name1, email1, password1, mobile1);
return null;
}
protected void onPostExecute(Double result) {
pb.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(),
"Account Activated Login To MyGenie",
Toast.LENGTH_LONG).show();
Intent intent = new Intent(RegisterActivity.this,
LoginActivity.class);
startActivity(intent);
}
protected void onProgressUpdate(Integer... progress) {
pb.setProgress(progress[0]);
}
public void postData(String name, String email, String password,
String mobile) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://yoursite.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new
ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name", name));
nameValuePairs.add(new BasicNameValuePair("email", email));
nameValuePairs
.add(new BasicNameValuePair("password", password));
nameValuePairs.add(new BasicNameValuePair("mobile", mobile));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
}
答案 1 :(得分:0)
除非您在手机上运行网络服务器和CakePHP,否则您可能需要使用运行网站的计算机的IP或主机名,而不是使用手机中的localhost。
<强>本地主机强> / AR /用户/登录名= m_sepehri&安培;密码= 111
和read this page关于CakePHP中的json视图。
同样以纯文本形式发送密码,而不是使用https作为奖励是疏忽。
答案 2 :(得分:0)
您可以使用Auth Comp。 CakePhp然后创建一个RESTFUL链接;在您的控制器USerController中创建一个方法,如一卡登录,然后通过POST
接收数据public function api_loginx(){
//$this->autoRender = false;
if ($this->request->is('post')) {
//print_r($this->request->data);
$this->request->data['User']['password'] =$_POST['password'];
$this->request->data['User']['username'] = $_POST['username'];
print_r($this->request->data);
print_r($this->request->data['User']);
debug($this->Auth->login());
if ($this->Auth->login()) {
echo "true";
//$this->Session->setFlash(__('Welcome, '. $this->Auth->user('username')));
//$this->redirect($this->Auth->redirectUrl());
} else {
echo "false";
//$this->Session->setFlash(__('Invalid username or password'));
}
}
}