0

我写了一个从用户那里获取用户名和密码的安卓登录应用程序。我想将用户名和密码发送到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 应用程序中工作!"localhost/ar/users/login?name=m_sepehri&password=111" 有人可以帮助我吗?

4

3 回答 3

1

您正在使用 httppost 但附加您的数据名称并传递到 url 本身。将 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
        }
    }

}
于 2013-09-25T07:09:19.837 回答
0

除非您在手机上运行网络服务器和 CakePHP,否则您可能希望使用运行网站的机器的 ip 或主机名,而不是使用手机中的 localhost。

本地主机/ar/users/login?name=m_sepehri&password=111

阅读此页面关于 CakePHP 中的 json 视图。

同样以纯文本形式发送密码并且作为奖励不使用 https 是疏忽大意的。

于 2013-08-15T11:58:27.387 回答
0

您可以使用 Auth Comp。CakePhp 的然后创建一个 RESTFULL 链接;在您的控制器 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'));
        }

    }
}
于 2014-04-30T14:59:02.460 回答