无法在android应用程序中获得php echo


Not able to get php echo in android app

我需要制作一个用于用户登录认证的app。

这是我的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";
这是我的android代码:
    @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部分??

问题是你的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