Java 通过url将数据从android应用程序发送到cakephp

Java 通过url将数据从android应用程序发送到cakephp,java,android,url,cakephp,http-post,Java,Android,Url,Cakephp,Http Post,我编写了一个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

我编写了一个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");
在浏览器中输入下面的url,这段代码运行良好,但在android应用程序中不起作用! “localhost/ar/users/login?name=m_sepheri&password=111”
有人可以帮我吗?

除了您在手机上运行Web服务器和CakePHP之外,您可能希望使用运行网站的机器的ip或主机名,而不是使用手机上的localhost

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

关于CakePHP中的json视图


另外,以纯文本形式发送密码,作为不使用https的奖励,这是一种疏忽。

您使用的是httppost,但添加了您的数据名并传递到url本身。将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
        }
    }

}
私有类MyAsyncTask扩展了AsyncTask{
@凌驾
受保护的双doInBackground(字符串…参数){
//TODO自动生成的方法存根
postData(姓名1、电子邮件1、密码1、手机1);
返回null;
}
受保护的void onPostExecute(双重结果){
pb.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(),
“帐户激活登录MyGenie”,
Toast.LENGTH_LONG).show();
意向意向=新意向(RegisterActivity.this,
物流活动(类);
星触觉(意向);
}
受保护的void onProgressUpdate(整数…进度){
pb.setProgress(progress[0]);
}
public void postData(字符串名称、字符串电子邮件、字符串密码、,
字符串(移动){
//创建一个新的HttpClient和Post头
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(
"http://yoursite.php");
试一试{
//添加您的数据
List nameValuePairs=新建
ArrayList();
添加(新的BasicNameValuePair(“name”,name));
添加(新的BasicNameValuePair(“email”,email));
nameValuePairs
.add(新的BasicNameValuePair(“密码”,password));
添加(新的BasicNameValuePair(“mobile”,mobile));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
HttpResponse response=httpclient.execute(httppost);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
}捕获(IOE异常){
//TODO自动生成的捕捉块
}
}
}

您可以使用认证组件。然后创建一个RESTFULL链接;在控制器USerController中,创建一个类似于caled登录的方法,然后通过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'));
        }

    }
}

我使用我的pc ip而不是localhost,并在我的设备上安装应用程序。我测试这个ip,没有问题。localhost是相对的。如果你在打你的手机,它的路由到127.0.0.1。。。在你的手机上。Apache的访问和错误日志将证明这一点。不,不,我使用计算机的ip,例如:192.168.1.1,而不是localhost,我在手机浏览器中看到了很好的效果,但我的问题是android应用程序。但在android代码中,您的硬编码ip是10.0.2.2。此外,顺便说一句,你的php代码非常难看。对其应用适当的格式,遵循CakePHP编码标准是一个好主意。还定义“不起作用”。这就像“医生,我病了”而不说什么痛。服务器是否正在接收请求?是否发送响应?android应用程序不能处理响应吗?php或apache错误日志中的任何内容…?服务器端代码在浏览器中运行,但没有android关联的结果。
    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);
        }
    }
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
        }
    }

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

    }
}