【问题标题】:Not able to get php echo in android app无法在android应用程序中获得php echo
【发布时间】:2015-09-15 13:58:41
【问题描述】:

我需要为用户登录认证制作 andoird 应用程序。

这是我的 php 代码:

<?php
     $con = mysql_connect('ip','root','pass') or die('cannot connect to db');
     mysql_select_db('master1') or die('cannot select db');
     if(isset($_POST['email']) && isset($_POST['pass'])) {      
          $email = $_POST['email'];
          $pass = $_POST['pass'];
          $query = "SELECT * FROM Users WHERE user_email='$email' AND user_password='$pass'";    
          $res=mysql_query($query);
     }

     if(mysql_num_rows($res) > 0) 
    echo "You are successfully logged in";
   else 
   echo "Incorrect email or password";

这是我的安卓代码:

    @Override
    protected String doInBackground(String... params) {
        InputStream is1 = null;
        for(String url1:params) {
            try {
                ArrayList<NameValuePair> pairs = new ArrayList<>();
                pairs.add(new BasicNameValuePair("email", etEmail.getText().toString()));
                pairs.add(new BasicNameValuePair("pass", etPass.getText().toString()));
                HttpClient client = new DefaultHttpClient();
                HttpPost post = new HttpPost(url1);
                post.setEntity(new UrlEncodedFormEntity(pairs));
                HttpResponse response = client.execute(post);
                is1 = response.getEntity().getContent();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                Toast.makeText(LoginActivity.this,e.toString(),Toast.LENGTH_SHORT).show();
            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(LoginActivity.this,e.toString(),Toast.LENGTH_SHORT).show();
            }
            BufferedReader reader = null;
            try {
                reader = new BufferedReader(new InputStreamReader(is1, "UTF-8"), 8);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                Toast.makeText(LoginActivity.this,e.toString(),Toast.LENGTH_SHORT).show();
            }
            String line = null;
            try {
                while ((line=reader.readLine())!=null) {
                    text += line + "\n";
                }
            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(LoginActivity.this,e.toString(),Toast.LENGTH_SHORT).show();
            }
            try {
                is1.close();
            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(LoginActivity.this,e.toString(),Toast.LENGTH_SHORT).show();
            }
        }
            return text;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        Toast.makeText(LoginActivity.this, s, Toast.LENGTH_SHORT).show();
        if (s.contains("You are successfully logged in")){
            Intent i = new Intent(LoginActivity.this,ConnectActivity.class);
            i.putExtra("user_email",etEmail.getText().toString());
            startActivity(i);
        }
        dialog.dismiss();
    }
}

问题是我在我的应用程序中获得了整个 php 脚本作为返回结果,我只需要 php echo,它可以有两个可能的值,正如您在我的代码中看到的那样,具体取决于用户插入的值。

有谁知道我的代码可能是什么问题,是在 php 部分还是在 android 部分???

【问题讨论】:

  • 你确定你有一个 web 服务器来提供 php 文件吗?听起来你只是在阅读 php 文件的内容。
  • 听起来您的网络服务器没有尝试解析 PHP。看看this question对你有没有帮助。
  • 您必须使用 json 编码而不是 echo,并在 android 中使用字符串生成器和流阅读器。
  • 我认为问题出在网络服务器上,当我修复该应用程序正常工作时

标签: php android login android-asynctask


【解决方案1】:

问题在于您的echo 语句,当您从 Android 访问 PHP 脚本时无法打印数据,您可以创建一个响应数组以将数据发送到 Android 应用程序,如下所示:

if(mysql_num_rows($res) > 0) {

  $response["success"] = 1;
  $response["message"] = "You are successfully logged in";
  echo json_encode($response);
}
else{
  $response["success"] = 0;
  $response["message"] = "Incorrect email or password";
  echo json_encode($response);
}

你应该参考这个链接: http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql

【讨论】:

  • 我浏览了您链接中的教程,但我发现这种方式有点困难,所以我编写了我的问题中可以看到的代码,当我修复 Web 服务器时,我得到了理想的结果。我不确定,我还是初学者,我想你的方式更好,但现在这个我的工作也在做。还是谢谢你的建议。
  • 以上链接将在您使用 JSON 并从服务器获取大量数据并将数据发送到服务器可能在未来提供帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多