AsyncTask insert data


        btn_registration.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                r_uname = edt_uname.getText().toString();
                r_email = edt_email.getText().toString();
                r_pass = edt_pass.getText().toString();
                r_conf_pass = edt_conf_pass.getText().toString();
                r_phone = edt_phone.getText().toString();
                r_text_date = temp_date.getText().toString(); //date nu text id feach karavu

                String country =spin_country.getSelectedItem().toString();
                String state =spin_state.getSelectedItem().toString();
                String city =spin_city.getSelectedItem().toString();

 if (edt_uname.length() > 0 && edt_email.length() > 0 && edt_pass.length() > 0 && edt_conf_pass.length() > 0 && edt_phone.length() > 0 && txt_display_date.length() > 0 && rbt_mail.isChecked() || rbt_femail.isChecked() )

                {
                    if(!"--Select Country--".equals(country) && !"--Select State--".equals(state) && !"--Select City--".equals(city)) {

                     //   Toast.makeText(Registraton.this, "Selected Country ", Toast.LENGTH_SHORT).show();


                        country1 = spin_country.getSelectedItem().toString();
                        state2 = spin_state.getSelectedItem().toString();
                        city3 = spin_city.getSelectedItem().toString();

                        if (rbt_gender.getCheckedRadioButtonId() == rbt_mail.getId()) {
                            chk_male = "Male";
                        } else if (rbt_gender.getCheckedRadioButtonId() == rbt_femail.getId()) {
                            chk_male = "Female";
                        }

                        String uploadImage = getStringImage(bitmap);
                        new Retrive().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, r_uname, r_email, r_pass, r_conf_pass, r_phone, r_text_date, chk_male, country1, state2, city3, uploadImage); //caling from niche ni method and string parmenter

                    }

                }
                else {

                    if (r_uname.length() == 0)
                    {
                        edt_uname.requestFocus();
                        edt_uname.setError("Enter User Name");
                    }

                    else if (!isValidEmail(r_email))
                    {


                        edt_email.requestFocus();
                        edt_email.setError("ENTER Email valide");
                    }
                    else if (r_pass.length() < 5)
                    {
                        edt_pass.requestFocus();
                        edt_pass.setError("Enter Password Must Be 5 char");
                    }
                    else if (!edt_pass.getText().toString().equals(edt_conf_pass.getText().toString()))  //conform password velidation textviee nu id levu

                    {
                        // else if (r_conf_pass.length() < 5) {
                        edt_conf_pass.requestFocus();
                        edt_conf_pass.setError("Enter Conform Password");
                    }

                    else if (r_conf_pass.length() < 5) {
                        edt_conf_pass.requestFocus();
                        edt_conf_pass.setError("Enter Conform Password");
                    }

                    else if (r_phone.length() < 10) {

                        edt_phone.requestFocus();
                        edt_phone.setError("Enter Must Be 10 Digit");
                    }
                    else if (temp_date.length() == 0)

                    {
                        AlertDialog alertDialog = new AlertDialog.Builder(Registraton.this).create();
                        alertDialog.setTitle("Alert");

                        alertDialog.setMessage("Select Your B.Date");
                        alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                });
                        alertDialog.show();

                        Toast.makeText(getApplicationContext(), "Select Your B.Date", Toast.LENGTH_SHORT).show();
                    }
                    else if (!rbt_mail.isChecked() || !rbt_femail.isChecked())
                    {
                        Toast.makeText(getApplicationContext(), "Please select Gender", Toast.LENGTH_SHORT).show();
                    }


                    else if("--Select Country--".equals(country))
                    {
                          Toast.makeText(Registraton.this, "Select Country ", Toast.LENGTH_SHORT).show();

                        AlertDialog alertDialog = new AlertDialog.Builder(Registraton.this).create();
                        alertDialog.setTitle("Alert");

                        alertDialog.setMessage("Select Image");
                        alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                });
                        alertDialog.show();
                    }
                    /*
                    else if("--Select State--".equals(state))
                    {
                          Toast.makeText(Registraton.this, "Select Country ", Toast.LENGTH_SHORT).show();

                    }
                    else if("--Select City--".equals(city))
                    {
                           Toast.makeText(Registraton.this, "Select Country ", Toast.LENGTH_SHORT).show();
                    }

             */       else if(upload_display_image.getDrawable() == null){

                    }

                    if (bitmap == null){

                        Toast.makeText(Registraton.this, "Select Image ", Toast.LENGTH_SHORT).show();

                       /* AlertDialog alertDialog = new AlertDialog.Builder(Registraton.this).create();
                        alertDialog.setTitle("Alert");

                        alertDialog.setMessage("Select Image");
                        alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                });
                        alertDialog.show();*/
                    }else{


                    }

                }


            }
        });

        checkLogin();



        //app start and get date currant
/*
        long date1 = System.currentTimeMillis();
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        String dateString = sdf.format(date1);
        txt_display_date.setText(dateString);
*/
        // end curant date


        //date image click date sate and display text view
        final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

                // TODO Auto-generated method stub
                myCalendar.set(Calendar.YEAR, year);
                myCalendar.set(Calendar.MONTH, monthOfYear);
                myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                updateLabel();
            }
        };

        date_image.setOnClickListener(new View.OnClickListener() { //image calender click on display date
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                new DatePickerDialog(Registraton.this, date, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                        myCalendar.get(Calendar.DAY_OF_MONTH)).show();
            }
        });


        //display button selected Image======================

        Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
        buttonLoadImage.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, "Select gallary Picture"), PICK_IMAGE_REQUEST);
            }
        });

        //clode image code ==========================

    }


    //check allread user name register or not
    //preference
    public void checkLogin() {
        LoginPreference loginpreference = new LoginPreference(getApplicationContext());
        String uname = loginpreference.get_uname();


        if (uname != null && uname.length() > 0) {
            Intent in = new Intent(getApplicationContext(), MainActivity.class);
            in.putExtra("Username", uname);
            in.putExtra("Email", r_email );
            startActivity(in);
            finish();
        }
    }


    private void updateLabel() {
        String myFormat = "dd/MM/yy"; //In which you need put here
        SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
        txt_display_date.setText(sdf.format(myCalendar.getTime()));
    }

    // end date image click date sate and display text view


    //image feach from storeg media========================

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {

            filePath = data.getData();
            try {
                bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
                    upload_display_image.setImageBitmap(bitmap);
                bitmap = Bitmap.createScaledBitmap(bitmap, 120, 120, false);


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

    public String getStringImage(Bitmap bmp) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] imageBytes = baos.toByteArray();
        String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
        return encodedImage;
    }

    /*close image code===========================*/

    //=================================================================web service ==========================

    class Retrive extends AsyncTask<String, Void, String> {
        ProgressDialog loading;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

          //  loading = ProgressDialog.show(Registraton.this, "Uploading..data.", null,true,true);
        }



        @Override
        protected String doInBackground(String... param) {
            String data = null;
            String success = null;
            try {

                data = URLEncoder.encode("uname", "UTF-8")      //text box  webservice ma je ceate karel che name deva and post man ma pan
                        + "=" + URLEncoder.encode(param[0], "UTF-8");

                data += "&" + URLEncoder.encode("email", "UTF-8")
                        + "=" + URLEncoder.encode(param[1], "UTF-8");

                data += "&" + URLEncoder.encode("password", "UTF-8")
                        + "=" + URLEncoder.encode(param[2], "UTF-8");

                data += "&" + URLEncoder.encode("conform_password", "UTF-8")
                        + "=" + URLEncoder.encode(param[3], "UTF-8");

                data += "&" + URLEncoder.encode("phone", "UTF-8")
                        + "=" + URLEncoder.encode(param[4], "UTF-8");

                data += "&" + URLEncoder.encode("p_date", "UTF-8") //date nu text id feach karavu id selected hase toj entery thase nahitar nai thai
                        + "=" + URLEncoder.encode(param[5], "UTF-8");

                data += "&" + URLEncoder.encode("gender", "UTF-8")  //gender data feach from textview array diff but fildname same rakhvana
                        + "=" + URLEncoder.encode(param[6], "UTF-8");


                data += "&" + URLEncoder.encode("country", "UTF-8")  //gender data feach from textview array diff but fildname same rakhvana
                        + "=" + URLEncoder.encode(param[7], "UTF-8");

                data += "&" + URLEncoder.encode("state", "UTF-8")  //gender data feach from textview array diff but fildname same rakhvana
                        + "=" + URLEncoder.encode(param[8], "UTF-8");

                data += "&" + URLEncoder.encode("city", "UTF-8")  //gender data feach from textview array diff but fildname same rakhvana
                        + "=" + URLEncoder.encode(param[9], "UTF-8");


                data += "&" + URLEncoder.encode("image", "UTF-8")  //image select data feach and upload data base
                        + "=" + URLEncoder.encode(param[10], "UTF-8");


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

            String text = "";
            BufferedReader reader = null;
            // Send data
            try {
                // Defined URL  where to send data
                URL url = new URL("http://sktechnocast.brijeshgohel.com/android/androidragistration.php");
                // Send POST data request
                URLConnection conn = url.openConnection();
                conn.setDoOutput(true);
                OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
                wr.write(data);
                wr.flush();
                // Get the server response
                reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                StringBuilder sb = new StringBuilder();
                String line = null;
                // Read Server Response
                while ((line = reader.readLine()) != null) {
                    // Append server response in string
                    sb.append(line + "\n");
                }
                text = sb.toString();
                try {

                    JSONObject j = new JSONObject(text);
                    success = j.getString("success");

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } catch (Exception ex) {

            } finally {
                try {

                    reader.close();
                } catch (Exception ex) {
                }
            }
            return success;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if (s.equals("1")) {
                Toast.makeText(Registraton.this, "successful", Toast.LENGTH_SHORT).show();

                Intent inttt = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(inttt);
            } else {
                Toast.makeText(Registraton.this, "Not registered or Allredy Register", Toast.LENGTH_SHORT).show();
            }
        }
    }

    // ===================================================================

Comments

Popular posts from this blog

retrofil gjstatus lanuage

form object

Login Preference in android create class