仅将ID添加到数据库,而未添加用户名,电子邮件和密码

时间:2018-06-24 13:40:01

标签: java android database

我正在尝试在android中创建注册系统,但由于某种原因,它只是将ID添加到了数据库,而没有用户名,电子邮件和密码。 This is how it added(last raw) PS。我很确定php文件自测试以来就可以正常工作

RegisterActivity.java

public class RegisterActivity extends AppCompatActivity {
TextView sign_in_text;
EditText Name,Email,Pass,ConPass;
Button reg_button;
AlertDialog.Builder mBuilder;

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

    sign_in_text = (TextView) findViewById(R.id.sign_in);
    sign_in_text.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(RegisterActivity.this,LoginActivity.class));
        }
    });

    Name = (EditText) findViewById(R.id.user_name);
    Email = (EditText) findViewById(R.id.email_register);
    Pass = (EditText) findViewById(R.id.password_register);
    ConPass = (EditText) findViewById(R.id.password_conf);
    reg_button = (Button) findViewById(R.id.reg_button);
    reg_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(Name.getText().toString().equals("")|| Email.getText().toString().equals("")|| Pass.getText().toString().equals("")){

                mBuilder = new AlertDialog.Builder(RegisterActivity.this);
                mBuilder.setTitle("Oops something went wrong!");
                mBuilder.setMessage("Please fill all the fields");
                mBuilder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                });

                AlertDialog alertDialog = mBuilder.create();
                alertDialog.show();

            }
            else if (!(Pass.getText().toString().equals(ConPass.getText().toString()))){


                mBuilder = new AlertDialog.Builder(RegisterActivity.this);
                mBuilder.setTitle("Oops something went wrong!");
                mBuilder.setMessage("Your passwords are not matching");
                mBuilder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                        Pass.setText("");
                        ConPass.setText("");
                    }
                });

                AlertDialog alertDialog = mBuilder.create();
                alertDialog.show();

            }

            else {

                BackgroundTask backgroundTask = new BackgroundTask(RegisterActivity.this);
                backgroundTask.execute("register",Name.getText().toString(),Email.getText().toString(),Pass.getText().toString());


            }
        }
    });
}

}

BackgroundTask.java

public class BackgroundTask extends AsyncTask<String, String, String> {
String register_url ="http://justawesome.net/ozone_registration/register.php";

String login_url ="http://justawesome.net/ozone_registration/login.php";
Context ctx;
ProgressDialog mProgressDialog;
Activity activity;
AlertDialog.Builder mBuilder;

public BackgroundTask(Context ctx){

    this.ctx = ctx;
    activity = (Activity) ctx;
}

@Override
protected void onPreExecute() {
    mBuilder = new AlertDialog.Builder(activity);
    mProgressDialog = new ProgressDialog(ctx);
    mProgressDialog.setTitle("Please wait");
    mProgressDialog.setMessage("Connecting to server....");
    mProgressDialog.setIndeterminate(true);
    mProgressDialog.setCancelable(false);
    mProgressDialog.show();

}

@Override
protected String  doInBackground(String... params) {

    String method = params[0];

    if (method.equals("register")){

        try {
            URL url = new URL(register_url);
            HttpURLConnection httpURLConnection =  (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter =new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));

            String name = params[1];
            String email = params[2];
            String password = params[3];

            String data = URLEncoder.encode("name", "UTF-8")+"-"+URLEncoder.encode(name,"UTF-8")+"&"+
                    URLEncoder.encode("email", "UTF-8")+"-"+URLEncoder.encode(email,"UTF-8")+"&"+
                    URLEncoder.encode("password", "UTF-8")+"-"+URLEncoder.encode(password,"UTF-8");

            bufferedWriter.write(data);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();

            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder stringBuilder = new StringBuilder();

            String line = "";
            while ((line=bufferedReader.readLine()) != null){

                stringBuilder.append(line+"\n");
            }

            httpURLConnection.disconnect();
            Thread.sleep(5000);
            return stringBuilder.toString().trim();


        } catch (MalformedURLException e) {
            e.printStackTrace();

        } catch (IOException e) {
            e.printStackTrace();

        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

谢谢您

1 个答案:

答案 0 :(得分:0)

String urlParameters  = "param1=a&param2=b&param3=c";
byte[] postData       = urlParameters.getBytes( StandardCharsets.UTF_8 );
int    postDataLength = postData.length;
String request        = "http://example.com/index.php";
URL    url            = new URL( request );
HttpURLConnection conn= (HttpURLConnection) url.openConnection();           
conn.setDoOutput( true );
conn.setInstanceFollowRedirects( false );
conn.setRequestMethod( "POST" );
conn.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded"); 
conn.setRequestProperty( "charset", "utf-8");
conn.setRequestProperty( "Content-Length", Integer.toString( postDataLength ));
conn.setUseCaches( false );
try( DataOutputStream wr = new DataOutputStream( conn.getOutputStream())) {
   wr.write( postData );
}

像这样发送数据!

如果仍然有问题,请发送您的php代码,也许问题在那里!