Safcode Institute by Sir Safder





  • Insert Data into Database

    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    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.Request;
    import com.android.volley.RequestQueue;
    import com.android.volley.Response;
    import com.android.volley.VolleyError;
    import com.android.volley.toolbox.StringRequest;
    import com.android.volley.toolbox.Volley;
    
    import java.util.HashMap;
    import java.util.Map;
    
    public class MainActivity extends AppCompatActivity {
    
        EditText name, email, password;
        String sname, semail, spassword;
        String url = "https://safcode.000webhostapp.com/conn.php";
        Button bt;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            name = (EditText) findViewById(R.id.name);
            email = (EditText) findViewById(R.id.email);
            password = (EditText) findViewById(R.id.password);
            bt = (Button) findViewById(R.id.button);
    
            bt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    sname = name.getText().toString().trim();
                    semail = email.getText().toString().trim();
                    spassword = password.getText().toString().trim();
    
                    StringRequest   rq = new StringRequest(Request.Method.POST, url, new Response.Listener() {
                        @Override
                        public void onResponse(String response) {
                            Toast.makeText(MainActivity.this, response, Toast.LENGTH_SHORT).show();
                        }
                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Toast.makeText(MainActivity.this, error.getMessage().toString(), Toast.LENGTH_SHORT).show();
                        }
                    }){
                        @Override
                        protected Map getParams() throws AuthFailureError {
                            //return super.getParams();
                            Map para = new HashMap();
                            para.put("name", sname);
                            para.put("email", semail);
                            para.put("password", spassword);
                            return para;
                        }
                    };
    
                    RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
                    requestQueue.add(rq);
    
                }
            });
    
        }
    }



  • You Can Also Watch Our Tutorial