如何连接到Android中xampp服务器中创建的数据库

时间:2015-05-26 10:52:37

标签: android web-services http xampp http-post

我已经下载了xamp服务器,我已经安装了它。我创建了一个名为" blogDB"的数据库。我创建了一个名为LoginTable的表,其中包含UID,用户名,密码,状态为列。现在从我的Android应用程序中,我需要在服务器中手动创建的数据库表中输入值。我发送了一个Http Post请求。我没有得到哪个网址应该传递给连接数据库。目前我有以下代码:

 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;
        }
    }

请让我知道我应该使用哪个网址连接到我的数据库以及如何在表格中输入数据。我很长时间都在苦苦挣扎。请帮我解决这个问题。

0 个答案:

没有答案