1

I have downloaded xamp server and I have installed it. I have created a database named "blogDB" and I have created a table with name LoginTable which has UID, username, password, status as columns. Now from my Android Application, I need to enter values to the database table created manually in the server. I am sending an Http Post request. I am not getting which url should I pass to connect to database. Currently I am having the following code:

 public void onClickLogin(View view) {

    // get The User name and Password
        enteredUserName = userName.getText().toString();
         enteredPassword = password.getText().toString();
   new ServerOperation().execute();

}

 public class ServerOperation extends AsyncTask {

        @Override
        protected Object doInBackground(Object[] params) {

            // Get user defined values
            enteredUserName = userName.getText().toString();
            enteredPassword = password.getText().toString();

            // Create data variable for sent values to server

            String data = null;
            try {
                data = URLEncoder.encode("name", "UTF-8")
                        + "=" + URLEncoder.encode(enteredUserName, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

            try {
                data += "&" + URLEncoder.encode("email", "UTF-8") + "="
                        + URLEncoder.encode(enteredPassword, "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://10.0.2.2/phpMyAdmin/tbl_addfield.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();
            }
            catch(Exception ex) {
                Log.e("iFocus", ""+ex);
            }
            try
            {

            }
            finally
            {
                try
                {

                    reader.close();
                }

                catch(Exception ex) {}
            }

            // Show response on activity
//            content.setText( text  );



            return null;
        }
    }

Please let me know what url should I use to connect to my database and how to enter data into the table. I am struggling from long time. Please help me come out of this problem.

keshav kowshik
  • 2,354
  • 4
  • 22
  • 45
  • Are you running your android application in the emulator and your xampp server is hosted in the the same machine? – Chatura Dilan May 26 '15 at 11:00
  • @ChaturaDilan Yes.. in my computer only. I am running application in emulator – keshav kowshik May 26 '15 at 11:01
  • You use an url to your php script. And then in the php script you place code to connect with your database, send data to it and retrieve data. When php is finished php echos data back to your client to tell results. – greenapps May 26 '15 at 11:06
  • @greenapps ok. then the insert operation must be done in the script itself? – keshav kowshik May 26 '15 at 11:07
  • Yes. Php does all the database actions. Your php script is te intermediate between your Android client and the database. – greenapps May 26 '15 at 11:08
  • @greenapps If you know any tutorial which has a good example can you please share the link? – keshav kowshik May 26 '15 at 11:09
  • If you read 50 pages on stackoverflow with tag android ( 50 per page also) you will find at least ten examples. – greenapps May 26 '15 at 11:10
  • @greenapps To get connected to php script do I need to use Http protocol? Where will I use that protocol exactly? – keshav kowshik May 26 '15 at 11:10
  • My god you have already that al in the code you posted. – greenapps May 26 '15 at 11:10
  • @greenapps Yes I know I have... I have very little knowledge about Http protocol.. but nil knowledge about php scripts so I am struggling.. – keshav kowshik May 26 '15 at 11:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/78789/discussion-between-keshav-and-greenapps). – keshav kowshik May 26 '15 at 11:19
  • @greenapps I do not understand for what Http Protocol is exactly used.. If we need to have a php script for insert etc operations what is the use of post,get,put,delete operations? – keshav kowshik May 26 '15 at 11:24
  • This is a site for programming questions. Not an IT school. Did you read your fifty pages already? – greenapps May 26 '15 at 11:31

0 Answers0