Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将本地db连接到本地Android应用程序?_Java_Android_Mysql_Database Connection - Fatal编程技术网

Java 如何将本地db连接到本地Android应用程序?

Java 如何将本地db连接到本地Android应用程序?,java,android,mysql,database-connection,Java,Android,Mysql,Database Connection,我创建了一个本地数据库。如何将其连接到我的应用程序 使用此public静态最终字符串LOGIN\u URL=”http://localhost:8888/phpmyadmin/tbl_export.php?db=androiddb&table=androiddb&server=1&target=&single_table=TRUE#PMAURL-1:tbl_structure.php?db=androiddb&table=androiddb&server=1&target=&token=e368

我创建了一个本地数据库。如何将其连接到我的应用程序

使用此
public静态最终字符串LOGIN\u URL=”http://localhost:8888/phpmyadmin/tbl_export.php?db=androiddb&table=androiddb&server=1&target=&single_table=TRUE#PMAURL-1:tbl_structure.php?db=androiddb&table=androiddb&server=1&target=&token=e3685a7b51239eb1312ab983d78e5a7b”未连接

我希望你能帮助我

提前谢谢大家

下面是我的.java类

LoginActivity.java:

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    //Defining views
    private EditText editTextEmail;
    private EditText editTextPassword;
    private AppCompatButton buttonLogin;

    //boolean variable to check user is logged in or not
    //initially it is false
    private boolean loggedIn = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        //Initializing views
        editTextEmail = (EditText) findViewById(R.id.editTextEmail);
        editTextPassword = (EditText) findViewById(R.id.editTextPassword);

        buttonLogin = (AppCompatButton) findViewById(R.id.buttonLogin);

        //Adding click listener
        buttonLogin.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //In onresume fetching value from sharedpreference
        SharedPreferences sharedPreferences = getSharedPreferences(Config.SHARED_PREF_NAME,Context.MODE_PRIVATE);

        //Fetching the boolean value form sharedpreferences
        loggedIn = sharedPreferences.getBoolean(Config.LOGGEDIN_SHARED_PREF, false);

        //If we will get true
        if(loggedIn){
            //We will start the Profile Activity
            Intent intent = new Intent(LoginActivity.this, ProfileActivity.class);
            startActivity(intent);
        }
    }

    private void login(){
        //Getting values from edit texts
        final String email = editTextEmail.getText().toString().trim();
        final String password = editTextPassword.getText().toString().trim();

        //Creating a string request
        StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.LOGIN_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        //If we are getting success from server
                        if(response.equalsIgnoreCase(Config.LOGIN_SUCCESS)){
                            //Creating a shared preference
                            SharedPreferences sharedPreferences = LoginActivity.this.getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE);

                            //Creating editor to store values to shared preferences
                            SharedPreferences.Editor editor = sharedPreferences.edit();

                            //Adding values to editor
                            editor.putBoolean(Config.LOGGEDIN_SHARED_PREF, true);
                            editor.putString(Config.EMAIL_SHARED_PREF, email);

                            //Saving values to editor
                            editor.commit();

                            //Starting profile activity
                            Intent intent = new Intent(LoginActivity.this, ProfileActivity.class);
                            startActivity(intent);
                        }else{
                            //If the server response is not success
                            //Displaying an error message on toast
                            Toast.makeText(LoginActivity.this, "Invalid username or password", Toast.LENGTH_LONG).show();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        //You can handle error here if you want
                    }
                }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> params = new HashMap<>();
                //Adding parameters to request
                params.put(Config.KEY_EMAIL, email);
                params.put(Config.KEY_PASSWORD, password);

                //returning parameter
                return params;
            }
        };

        //Adding the string request to the queue
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }

    @Override
    public void onClick(View v) {
        //Calling the login function
        login();
    }
}

如果您使用的是仿真器,则可以将“localhost”替换为“10.0.2.2”。但是,如果您需要在真实设备上测试它,那么您需要一个主机,或者您可以使用栓系连接。

如果您使用的是仿真器,您可以将“localhost”替换为“10.0.2.2”。但是如果你需要在真实的设备上测试它,那么你需要一个主机或者你可以用拴绳连接。

好的,现在我尝试使用emulator…但是我没有从phpmyadmin导出任何文件?我的做法是正确的?如果你可以从浏览器中发出成功请求,那么你可以在android上做同样的事情。因为它只是一个“客户端”:)这是有联系的,因为当我点击“登录”按钮时,告诉我“用户名或密码不正确”,但在db中,我创建了一个字段“ciccia”和另一个字段“ciccia”,我不知道为什么对webservice的请求不正确,我个人更喜欢使用名为Reformation的库。这是一个更简单的安全线程。好吧,现在我尝试使用emulator…但我不从phpmyadmin导出任何文件?我的做法是正确的?如果你可以从浏览器中发出成功请求,那么你可以在android上做同样的事情。因为它只是一个“客户端”:)这是有联系的,因为当我点击“登录”按钮时,告诉我“用户名或密码不正确”,但在db中,我创建了一个字段“ciccia”和另一个字段“ciccia”,我不知道为什么对webservice的请求不正确,我个人更喜欢使用名为Reformation的库。它是更简单的安全线程。
public class Config {
    public static final String LOGIN_URL = "http://localhost:8888/phpmyadmin/tbl_export.php?db=androiddb&table=androiddb&server=1&target=&single_table=TRUE#PMAURL-1:tbl_structure.php?db=androiddb&table=androiddb&server=1&target=&token=e3685a7b51239eb1312ab983d78e5a7b";

    //Keys for email and password as defined in our $_POST['key'] in login.php
    public static final String KEY_EMAIL = "email";
    public static final String KEY_PASSWORD = "password";

    //If server response is equal to this that means login is successful
    public static final String LOGIN_SUCCESS = "success";

    //Keys for Sharedpreferences
    //This would be the name of our shared preferences
    public static final String SHARED_PREF_NAME = "myloginapp";

    //This would be used to store the email of current logged in user
    public static final String EMAIL_SHARED_PREF = "email";

    //We will use this to store the boolean in sharedpreference to track user is loggedin or not
    public static final String LOGGEDIN_SHARED_PREF = "loggedin";
}