dimanche 27 décembre 2015

No data inserted to MySQL from Android

In my app, I need the data insert to two places when register button is clicked, one is to SQLite and another is to MySQL (Xampp server). Data are successfully added into SQLite but no luck with MySQL.

Register

 private static final String REGISTER_URL = "http://ift.tt/1kmYtC9";

     register.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                   Password=password.getText().toString();
                   ConfirmPassword=confirmPassword.getText().toString();
                        sqlcon.open();
                        sqlcon.insertEntry(deviceName, Password); // insert to SQLite, no problem
                        register(deviceName, Password); // insert to MySQL

                    }

                }
            });    
        }

        private void register(String name,String password) {
            class RegisterUser extends AsyncTask<String, Void, String> {
                ProgressDialog loading;
                RequestHandler ruc = new RequestHandler();


                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                    loading = ProgressDialog.show(Register.this, "Please Wait",null, true, true);
                }

                @Override
                protected void onPostExecute(String s) {
                    super.onPostExecute(s);
                    loading.dismiss();
                    Toast.makeText(getApplicationContext(),"AA"+s,Toast.LENGTH_LONG).show();
                }

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

                    HashMap<String, String> data = new HashMap<String,String>();
                    data.put("name",params[0]);
                    data.put("password",params[1]);
                    String result = ruc.sendPostRequest(REGISTER_URL,data);
                    return  result;
                }
            }

            RegisterUser ru = new RegisterUser();
            ru.execute(name,password);
        }

RequestHandler

public class RequestHandler {

        public String sendPostRequest(String requestURL,
                                      HashMap<String, String> postDataParams) {

            URL url;
            StringBuilder sb = new StringBuilder();
            try {
                //Initializing Url
                url = new URL(requestURL);

                //Creating an httmlurl connection
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();

                //Configuring connection properties
                conn.setReadTimeout(15000);
                conn.setConnectTimeout(15000);
                conn.setRequestMethod("POST");
                conn.setDoInput(true);
                conn.setDoOutput(true);

                //Creating an output stream
                OutputStream os = conn.getOutputStream();

                //Writing parameters to the request
                //We are using a method getPostDataString which is defined below
                BufferedWriter writer = new BufferedWriter(
                        new OutputStreamWriter(os, "UTF-8"));
                writer.write(getPostDataString(postDataParams));

                writer.flush();
                writer.close();
                os.close();
                int responseCode = conn.getResponseCode();

                if (responseCode == HttpsURLConnection.HTTP_OK) {

                    BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                    sb = new StringBuilder();
                    String response;
                    //Reading server response
                    while ((response = br.readLine()) != null){
                        sb.append(response);
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
            return sb.toString();
        }

        public String sendGetRequest(String requestURL){
            StringBuilder sb =new StringBuilder();
            try {
                URL url = new URL(requestURL);
                HttpURLConnection con = (HttpURLConnection) url.openConnection();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));

                String s;
                while((s=bufferedReader.readLine())!=null){
                    sb.append(s+"\n");
                }
            }catch(Exception e){
            }
            return sb.toString();
        }

        public String sendGetRequestParam(String requestURL, String id){
            StringBuilder sb =new StringBuilder();
            try {
                URL url = new URL(requestURL+id);
                HttpURLConnection con = (HttpURLConnection) url.openConnection();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));

                String s;
                while((s=bufferedReader.readLine())!=null){
                    sb.append(s+"\n");
                }
            }catch(Exception e){
            }
            return sb.toString();
        }

        private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
            StringBuilder result = new StringBuilder();
            boolean first = true;
            for (Map.Entry<String, String> entry : params.entrySet()) {
                if (first)
                    first = false;
                else
                    result.append("&");

                result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
                result.append("=");
                result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
            }

            return result.toString();
        }
    }

When register button is clicked, I saw the Toast message displayed which is inside the Register onPostExecute.

In RequestHandler, sendGetRequest,sendGetRequestParam is never used ! When check in MySQL, no data is inserted.

Aucun commentaire:

Enregistrer un commentaire