简体   繁体   中英

Unable to connect Wamp MySQL database to Android Studio

I have tried so many different IP Addresses as well as using 127.0.0.0 and my own computer IP Address, however when I run my program and try to register it contacts the server and then it suddenly stops and it says that the app name has stopped working. Please could you help me or give me a helping hand as I have been stuck on this issue for three days.

I have created a folder in wamp/www/ folder called bradvisor_login_api, this folder has three files and one of the file is for the config.php file which I have listed below. phpMyAdmin account does not have a password it only has username which is root.

Could anyone please help me to resolve this issue, I have pasted my config.php file and some of my java file where it shows the connection.

Config.PHP File

<?php

/**
 * Database config variables
 */
define("DB_HOST", "127.0.0.1");
define("DB_USER", "root");
define("DB_PASSWORD", "root");
define("DB_DATABASE", "bradvisor_login_api");
?>

Java connection file

//URL of the PHP API


   private static String loginURL = "http://192.168.0.8:80/bradvisor_login_api/";
    private static String registerURL = "http://192.168.0.8:80/bradvisor_login_api/";
    private static String forpassURL = "http://192.168.0.8:80/bradvisor_login_api/";
    private static String chgpassURL = "http://192.168.0.8:80/bradvisor_login_api/";

httpd.conf File

# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, instead of the default. See also the <VirtualHost>
# directive.
#
# Change this to Listen on specific IP addresses as shown below to 
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:80
Listen 0.0.0.0:80
Listen [::0]:80

Error Logcat

  07-02 20:02:42.163    2269-2526/com.brad.visor E/Buffer Error﹕ Error converting result java.lang.NullPointerException: lock == null
07-02 20:02:42.163    2269-2526/com.brad.visor E/JSON Parser﹕ Error parsing data org.json.JSONException: End of input at character 0 of
07-02 20:02:42.194    2269-2269/com.brad.visor E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.brad.visor, PID: 2269
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.getString(java.lang.String)' on a null object reference
            at com.brad.visor.Register$ProcessRegister.onPostExecute(Register.java:232)
            at com.brad.visor.Register$ProcessRegister.onPostExecute(Register.java:189)
            at android.os.AsyncTask.finish(AsyncTask.java:636)
            at android.os.AsyncTask.access$500(AsyncTask.java:177)
            at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Register.java File:

private class ProcessRegister extends AsyncTask<String, String, JSONObject>
------------------------------------------------------------------------
{

        /**
         * Defining Process dialog
         **/
        private ProgressDialog pDialog;

        String email,password,fname,lname,uname;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            inputUsername = (EditText) findViewById(R.id.uname);
            inputPassword = (EditText) findViewById(R.id.pword);
            fname = inputFirstName.getText().toString();
            lname = inputLastName.getText().toString();
            email = inputEmail.getText().toString();
            uname= inputUsername.getText().toString();
            password = inputPassword.getText().toString();
            pDialog = new ProgressDialog(Register.this);
            pDialog.setTitle("Contacting Servers");
            pDialog.setMessage("Registering ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected JSONObject doInBackground(String... args) {

            UserFunctions userFunction = new UserFunctions();
            JSONObject json = userFunction.registerUser(fname, lname, email, uname, password);

            return json;

        }
        @Override
        protected void onPostExecute(JSONObject json) {
            /**
             * Checks for success message.
             **/
            try {

if (json.getString(KEY_SUCCESS) != null) {
------------------------------------------
                    registerErrorMsg.setText("");
                    String res = json.getString(KEY_SUCCESS);

                    String red = json.getString(KEY_ERROR);

                    if(Integer.parseInt(res) == 1){
                        pDialog.setTitle("Getting Data");
                        pDialog.setMessage("Loading Info");

                        registerErrorMsg.setText("Successfully Registered");

                        DatabaseHandler db = new DatabaseHandler(getApplicationContext());
                        JSONObject json_user = json.getJSONObject("user");

                        /**
                         * Removes all the previous data in the SQlite database
                         **/

                        UserFunctions logout = new UserFunctions();
                        logout.logoutUser(getApplicationContext());
                        db.addUser(json_user.getString(KEY_FIRSTNAME),json_user.getString(KEY_LASTNAME),json_user.getString(KEY_EMAIL),json_user.getString(KEY_USERNAME),json_user.getString(KEY_UID),json_user.getString(KEY_CREATED_AT));
                        /**
                         * Stores registered data in SQlite Database
                         * Launch Registered screen
                         **/

                        Intent registered = new Intent(getApplicationContext(), Registered.class);

                        /**
                         * Close all views before launching Registered screen
                         **/
                        registered.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        pDialog.dismiss();
                        startActivity(registered);

                        finish();
                    }

                    else if (Integer.parseInt(red) ==2){
                        pDialog.dismiss();
                        registerErrorMsg.setText("User already exists");
                    }
                    else if (Integer.parseInt(red) ==3){
                        pDialog.dismiss();
                        registerErrorMsg.setText("Invalid Email id");
                    }
                }
                else{
                    pDialog.dismiss();

                    registerErrorMsg.setText("Error occured in registration");
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }}
    public void NetAsync(View view){
        new NetCheck().execute();
    }}

From your error log: "Value < br of type java.lang.String cannot be converted to JSONObject" What is the page you are requesting, and can you post its contents in the question.

As far as I can tell from the error log, the input is not parse-able. This seems to be the case when you receive an response as HTML (where the breakrow is the < br /> indicator, but on strings, they could be several, as \\0 for instance. It does seems that when parsing, the content is read as a single variable, and fails to buffer the whole response in the variable, then, that failed object becomes null, and you make an operation on it. (thus failing, and getting the logged error)

in Android Studio you have to put 10.0.2.2 as IP. It's the ip Android Studio use for localhost connection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM