为什么我的Android应用程序会将空白记录插入MySQL数据库?

时间:2017-02-20 22:13:27

标签: java php android mysql android-studio

目前,我正致力于通过Android Studio开发应用程序,并致力于编写登录和注册功能。我们通过PHP将新注册用户的信息外部(即非本地)发送到MySQL数据库,并且还希望将注册用户登录到应用程序中。目前,Android应用程序没有将用户输入的信息发送到PHP文件到文本框中。我将一个跟踪器放入PHP文件中,它显示值为null。

这是我的MainActivity.java:

List<Desk>

这是我的SignupActivity.java:

package android.capstone.registerlogin;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import static android.capstone.registerlogin.R.id.etCreateAccEmail;
import static android.capstone.registerlogin.R.id.etCreateAccPassword;
import static android.capstone.registerlogin.R.id.etCreateAccUsername;


public class MainActivity extends Activity {

    private EditText etUserName, etPassWord, etEmail;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_login);

        etPassWord = (EditText) findViewById(etCreateAccPassword);
        etUserName = (EditText) findViewById(etCreateAccUsername);
        etEmail = (EditText) findViewById(etCreateAccEmail);
        final TextView createAcc = (TextView) findViewById(R.id.tvCreateAcc);
        createAcc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /* creating this intent shows where you're starting and
                where you want to go, we want to get the to the create account page
                called RegisterUser.java
                   first line creates the intent
                 */
                Intent createIntent = new Intent(MainActivity.this, CreateAccount.class);
                //second line makes the intent actually happen .startActivity
                MainActivity.this.startActivity(createIntent);
            }
        });

    }



    public void signup(View v) {
        String UserName = etUserName.getText().toString();
        String PassWord = etPassWord.getText().toString();
        String Email = etEmail.getText().toString();



        Toast.makeText(this, "Signing up...", Toast.LENGTH_SHORT).show();

        new SignupActivity(this).execute(UserName, PassWord, Email);
    }


    }

这是我的CreateAccount.java:

package android.capstone.registerlogin;

import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class SignupActivity extends AsyncTask<String, Void, String> {
    private Context context;

    public SignupActivity(Context context) {
        this.context = context;
    }

    protected void onPreExecute() {

    }

    @Override
    protected String doInBackground(String... arg0) {
        String userName = arg0[0];
        String passWord = arg0[1];
        String eMail = arg0[2];



        String link;
        String data;
        BufferedReader bufferedReader;
        String result;

        try {
            data = "?username=" + URLEncoder.encode(userName, "UTF-8");
            data += "&password=" + URLEncoder.encode(passWord, "UTF-8");
            data += "&email=" + URLEncoder.encode(eMail, "UTF-8");



            link = "http://cgi.soic.indiana.edu/~gabschle/signup.php" + data;
            URL url = new URL(link);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();

            bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
            result = bufferedReader.readLine();
            return result;
        } catch (Exception e) {
            return new String("Exception: " + e.getMessage());
        }
    }

    @Override
    protected void onPostExecute(String result) {
        String jsonStr = result;
        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);
                String query_result = jsonObj.getString("query_result");
                if (query_result.equals("SUCCESS")) {
                    Toast.makeText(context, "Data inserted successfully. Signup successful.", Toast.LENGTH_SHORT).show();
                } else if (query_result.equals("FAILURE")) {
                    Toast.makeText(context, "Data could not be inserted. Signup failed." + jsonStr, Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(context, "Couldn't connect to remote database.", Toast.LENGTH_SHORT).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
                Toast.makeText(context, "Error parsing JSON data.", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(context, "Couldn't get any JSON data.", Toast.LENGTH_SHORT).show();
        }
    }
}

这是我的PHP文件:

package android.capstone.registerlogin;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import static android.capstone.registerlogin.R.id.etCreateAccEmail;
import static android.capstone.registerlogin.R.id.etCreateAccPassword;
import static android.capstone.registerlogin.R.id.etCreateAccUsername;

public class CreateAccount extends Activity {


    private EditText etUserName, etPassWord, etEmail;
    private Button mButton;
    //private Context context;

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

        etPassWord = (EditText) findViewById(etCreateAccPassword);
        etUserName = (EditText) findViewById(etCreateAccUsername);
        etEmail = (EditText) findViewById(etCreateAccEmail);

        final String UserName = etUserName.getText().toString();
        final String PassWord = etPassWord.getText().toString();
        final String Email = etEmail.getText().toString();


        mButton = (Button) findViewById(R.id.bCreateAccSignUp);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //Toast.makeText(this, "Signing up...", Toast.LENGTH_SHORT).show();
                new SignupActivity(CreateAccount.this).execute(UserName, PassWord, Email);


            }

            });
        }




    public void signup(View v) {
        String UserName = etUserName.getText().toString();
        String PassWord = etPassWord.getText().toString();
        String Email = etEmail.getText().toString();



        Toast.makeText(this, "Signing up...", Toast.LENGTH_SHORT).show();
        new SignupActivity(this).execute(UserName, PassWord, Email);
    }
}

如果您需要其他文件,请告诉我们! 谢谢你的帮助!

1 个答案:

答案 0 :(得分:1)

您在分配之前(在onCreate期间)从文本字段中获取值。

更改为:

    mButton = (Button) findViewById(R.id.bCreateAccSignUp);
    mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String UserName = etUserName.getText().toString();
            String PassWord = etPassWord.getText().toString();
            String Email = etEmail.getText().toString();
            //Toast.makeText(this, "Signing up...", Toast.LENGTH_SHORT).show();
            new SignupActivity(CreateAccount.this).execute(UserName, PassWord, Email);
        }
    });

ps:SignupActivity中的context是内存泄漏。