Java 无法创建Android PHP MySQL登录应用程序

Java 无法创建Android PHP MySQL登录应用程序,java,php,android,mysql,Java,Php,Android,Mysql,我正在创建一个AndroidPHPMySQL登录应用程序,它将从MySQL服务器检查用户名和密码。我从学校得到了教程 我已经在MySQL中检查了名称和密码,它与什么用户类型匹配,但我仍然无法进入主页活动,因为它显示无效的用户名或密码。 private void login(final String username, final String password) { class LoginAsync extends AsyncTask<String, Void, Str

我正在创建一个
Android
PHP
MySQL
登录应用程序,它将从MySQL服务器检查用户名和密码。我从学校得到了教程

我已经在
MySQL
中检查了名称和密码,它与什么用户类型匹配,但我仍然无法进入主页活动,因为它显示无效的用户名或密码。

 private void login(final String username, final String password) {

        class LoginAsync extends AsyncTask<String, Void, String> {

            private Dialog loadingDialog;

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                loadingDialog = ProgressDialog.show(MainActivity.this, "Please wait", "Loading...");
            }

            @Override
            protected String doInBackground(String... params) {
                InputStream is = null;
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("username", username));
                nameValuePairs.add(new BasicNameValuePair("password", password));
                String result = null;

                try{
                    HttpClient httpClient = new DefaultHttpClient();
                    HttpPost httpPost = new HttpPost(
                            "http://192.168.1.7:80/Android/CRUD/login.php");
                    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                    HttpResponse response = httpClient.execute(httpPost);

                    HttpEntity entity = response.getEntity();

                    is = entity.getContent();

                    BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
                    StringBuilder sb = new StringBuilder();

                    String line = null;
                    while ((line = reader.readLine()) != null)
                    {
                        sb.append(line + "\n");
                    }
                    result = sb.toString();
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return result;
            }

            @Override
            protected void onPostExecute(String result){
                String s = result.trim();
                loadingDialog.dismiss();
                if(s.equalsIgnoreCase("success")){
                    Intent intent = new Intent(MainActivity.this, HomePage.class);
                    //intent.putExtra(USER_NAME, username);
                    finish();
                    startActivity(intent);
                }else {
                    Toast.makeText(getApplicationContext(), "Invalid User Name or Password", Toast.LENGTH_LONG).show();
                }
            }
        }

        LoginAsync la = new LoginAsync();
        la.execute(username, password);

    }
private void登录(最终字符串用户名、最终字符串密码){
类LoginAsync扩展了AsyncTask{
私有对话框加载对话框;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
loadingDialog=ProgressDialog.show(MainActivity.this,“请稍候”,“正在加载…”);
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
InputStream=null;
List nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“用户名”,username));
添加(新的BasicNameValuePair(“密码”,password));
字符串结果=null;
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(
"http://192.168.1.7:80/Android/CRUD/login.php");
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpClient.execute(httpPost);
HttpEntity=response.getEntity();
is=entity.getContent();
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is,“UTF-8”),8;
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null)
{
sb.追加(第+行“\n”);
}
结果=sb.toString();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
返回结果;
}
@凌驾
受保护的void onPostExecute(字符串结果){
字符串s=result.trim();
loadingDialog.disclose();
如果(s.equalsIgnoreCase(“成功”)){
意向意向=新意向(MainActivity.this,HomePage.class);
//intent.putExtra(用户名、用户名);
完成();
星触觉(意向);
}否则{
Toast.makeText(getApplicationContext(),“无效用户名或密码”,Toast.LENGTH_LONG.show();
}
}
}
LoginAsync la=新LoginAsync();
la.执行(用户名、密码);
}
login.php

<? php
require_once("dbConnect.php");

$con = mysqli_connect(HOST,USER,PASS,DB);

$username = $_POST['name'];
$password = $_POST['password'];

$sql = "select * from users where name='$username' and password='$password'";

$res = mysqli_query($con,$sql);

$check = mysqli_fetch_array($res);

if(isset($check)){
echo 'success';
}else{
echo 'failure';
}

mysqli_close($con);
?>

换成这个:

$username = $_POST['username'];
换成这个:

$username = $_POST['username'];

首先检查日志中的响应

protected void onPostExecute(String result){
            String s = result.trim();
            Log.d("response: ", s);
        }

首先检查日志中的响应

protected void onPostExecute(String result){
            String s = result.trim();
            Log.d("response: ", s);
        }


使用json编码,因为在您的代码中可能会返回一些无效信息character@Rasel如何编码?尝试使用fiddler2捕获HTTP请求以进行分析如果您获得HTTP请求包,也许您可以将其添加到您的问题中。我认为它可以为你提供更多的信息,让更多的人帮助你更容易。需要编码,因为问题是你的php我认为。如果你编码它,你可以防止这种类型的结果使用json编码,因为在你的代码中可能会返回一些无效character@Rasel如何编码?尝试使用fiddler2捕获HTTP请求以分析如果您得到http请求包,也许您可以将其添加到您的问题中。我认为它可以为你提供更多的信息,让更多的人帮助你更容易。需要编码,因为问题是你的php我认为。如果你编码它,你可以防止这种类型的结果显示我所有的phpso问题是在你的phpIf你使用本地主机+模拟器使用url。。。或者json编码你的response@AshutoshVerma我该怎么办?@Hoo首先使用HttpPost-HttpPost=new-HttpPost(“),如果它不起作用,请使用json对您的响应进行编码。它显示我的所有phpso问题都在您的phpIf中。如果您使用的是localhost+emulator使用url…或json对您的响应进行编码response@AshutoshVerma我该怎么办?@Hoo首先使用HttpPost-HttpPost=new-HttpPost(“),如果它不起作用,请对您的响应进行json编码。