Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

Como adicionar dados de entrada edittext no banco de dados sql usando json, android?


Atualize seu createNewProduct para isso:
class CreateNewProduct extends AsyncTask<String, String, String> {
   private  String fname;
   private  String lname;
   private  String email;

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(RegistrationForm.this);
        pDialog.setMessage("Creating books..");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
        fname = fn.getText().toString();
        lname = ln.getText().toString();
        email = em.getText().toString();
    }




    protected String doInBackground(String... args) {


        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("First_Name", fname));
        params.add(new BasicNameValuePair("Last_Name",lname));
        params.add(new BasicNameValuePair("email", email));


        // getting JSON Object
        // Note that create product url accepts POST method
        JSONObject json = jsonParser.makeHttpRequest(url_create_book,
                "POST", params);

        // check log cat fro response
        Log.d("Create Response", json.toString());

        // check for success tag
        try {
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // successfully created product
                Intent i = new Intent(getApplicationContext(), Login.class);
                startActivity(i);

                // closing this screen
                finish();
            } else {
                // failed to create product
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog once done
        pDialog.dismiss();
    }

}

Em AsyncTask, onPreExecute e onPostExecute são executados no encadeamento principal, enquanto doInBackground é executado no encadeamento em segundo plano, no qual você não pode executar nenhuma operação relacionada à interface do usuário.