0

I wrote one android login app that get username and password from user. I whant to send username & password to server that is cakephp application.This is my android code:

    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");

I write only this line in layout:

         <?php   echo $content_for_layout ?>

view code:

        <?php echo json_encode($user); ?>

controller:

    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);
        }
    }

this code work goodly by enter below url in browser but don't work in android app! "localhost/ar/users/login?name=m_sepehri&password=111" anybody can help me?

3 Answers 3

1

You are using httppost but appending your data name and pass into the url itself. Use AsyncTask with List as follows:

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
        }
    }

}
Sign up to request clarification or add additional context in comments.

Comments

0

Except you're running a webserver and CakePHP on your phone you might want to use the ip or hostname of the machine running the website instead of using localhost from your phone.

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

And read this page about json views in CakePHP.

Also sending the password in plain text and as a bonus not using https is negligent.

5 Comments

I use my pc ip instead of localhost and install app on my device.I test this ip,no problem in this.
"localhost" is relative. If you're hitting localhost/adm/controller/action on your phone, it's routing to 127.0.0.1... on your phone. Apache's access and error log will prove it.
No no, i use computer's ip such: 192.168.1.1 instead of localhost and i seen good result in browser of phone, but my problem is android app.
But your hardcoded ip is 10.0.2.2 in your android code. Besides that your php code is pretty ugly by the way. Apply proper formatting to it, following the CakePHP coding standard is a good idea. Also define "does not work". That's like "doctor I'm sick" without telling what hurts. Is the server receiving a request? Is a response sent or not? Can't the android app handle the response? Anything in php or apache error log...?
server side code is working in browser but no result in association by android.
0

You can use the Auth Comp. of CakePhp Then create a RESTFULL link ; in your controller USerController create a method like the one caled login then receive data via 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'));
        }

    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.