0

ユーザーからユーザー名とパスワードを取得するAndroidログインアプリを1つ作成しました。ユーザー名とパスワードを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 自体に渡します。次のように List で AsyncTask を使用します。

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

電話で Web サーバーと CakePHP を実行している場合を除き、電話から localhost を使用する代わりに、Web サイトを実行しているマシンの IP またはホスト名を使用したい場合があります。

localhost /ar/users/login?name=m_sepehri&password=111

そして、CakePHP の json ビューに関するこのページを読んでください。

また、パスワードを平文で送信し、おまけとして https を使用しないのは過失です。

于 2013-08-15T11:58:27.387 に答える
0

Auth Comp を使用できます。次に、RESTFULL リンクを作成します。コントローラー USerController で login と呼ばれるメソッドを作成し、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 に答える