Я пытаюсь создать Вход с помощью localhost - PullRequest
0 голосов
/ 27 сентября 2019

Я хочу создать приложение опроса, но оно застряло на if (con == null) {z = "Please check your internet connection"; Я искал в Google, и я новичок в Java, я знаю, это означает, что приложение еще не подключено к соединению, но я нене знаю, где я ошибся

это Connectionclass.java для подключения к localhost


import android.annotation.SuppressLint;
import android.os.StrictMode;
import android.util.Log;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionClass {

    @SuppressLint("NewAPI")
    public Connection Conn() {
    String kelas = "com.mysql.jdbc.Driver";

    String url = "jdbc:mysql://10.0.2.2:3306/kartu_tani";
    String uname = "";
    String password = "";
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);
        Connection connection = null;
        try {
            Class.forName(kelas).newInstance();
             connection = DriverManager.getConnection(url, uname, password);
        } catch (ClassNotFoundException e) {
            Log.e("ERROR", e.getMessage());
        } catch (SQLException e) {
            Log.e("ERROR", e.getMessage());
        } catch (Exception e) {
            Log.e("ERROR", e.getMessage());
        }
        return connection;
    }
}

и это страница входа


import androidx.appcompat.app.AppCompatActivity;

import android.app.DownloadManager;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.telecom.Call;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.e_survey.Background;
import com.example.e_survey.ConnectionClass;
import com.example.e_survey.Model.ProfilDesa;
import com.example.e_survey.R;
import com.example.e_survey.Util.Constant;
import com.example.e_survey.Util.SharedPreferenceCustom;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

import static com.bumptech.glide.request.Request.*;

public class LoginActivity extends AppCompatActivity {

    Button btnLogin;
    SharedPreferenceCustom sharedPreferenceCustom;
    EditText etUsername,etPassword;
    ConnectionClass connectionClass;
    ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        etUsername = findViewById(R.id.et_username);
        etPassword = findViewById(R.id.et_password);
        btnLogin = findViewById(R.id.btnLogin);
        connectionClass = new ConnectionClass();
        progressDialog = new ProgressDialog(this);
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doLogin doLogin = new doLogin();
                doLogin.execute("");
            }
        });
        /*sharedPreferenceCustom = SharedPreferenceCustom.getInstance(this);
        initProgresDialog();
        initFindView();*/
    }
    /*private void initProgresDialog() {
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Please Wait...");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setCancelable(false);
    }*/

    private class doLogin extends AsyncTask<String, String, String> {
        String username = etUsername.getText().toString();
        String password = etPassword.getText().toString();
        String z = "";
        boolean isSuccess = false;
        String un, pw;

        @Override
        protected void onPreExecute() {
            progressDialog.setMessage("Please Wait");
            progressDialog.show();

            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... params) {
            if (username.trim().equals("")) {
                z = "Masukkan Username Anda!";
            } else if (password.trim().equals("")) {
                z = "Masukkan Password Anda!";
            } else {
                try {
                    ConnectionClass db = new ConnectionClass();
                    Connection con = db.Conn();

                    if (con == null) {
                        z = "Please check your internet connection";
                    } else {

                        String query = "select * from userlogin where username = '"+ username +"' and password = '"+ password +"'";

                        Statement stm = con.createStatement();
                        ResultSet rs = stm.executeQuery(query);

                        while (rs.next()) {
                            un = rs.getString(1);
                            pw = rs.getString(2);

                            if (un.equals(username) && pw.equals(password)) {
                                isSuccess = true;
                                z = "Login Successfull";
                            } else {
                                isSuccess = false;
                            }
                        }
                    }
                } catch (Exception e) {
                    isSuccess = false;
                    z = "Exception" + e;
                }
            }
            return z;
        }

        @Override
        protected void onPostExecute(String s) {
            Toast.makeText(getBaseContext(), "" + z, Toast.LENGTH_LONG).show();

            if (isSuccess) {

                Intent intent = new Intent(LoginActivity.this, ProfilDesaActivity.class);
                startActivity(intent);
            }

            progressDialog.hide();
        }
    }
}

спасибо

...