简体   繁体   English

Android移动应用程序连接到Web主机数据库

[英]Android Mobile Application Connection to a Web Host Database

I'm currently creating a mobile application for a website wherein I have to connect to a database uploaded on the web host. 我正在为网站创建一个移动应用程序,我必须连接到在Web主机上上传的数据库。 I have few questions on how I should start. 关于我应该如何开始,我几乎没有问题。 I have knowledge in Android Eclipse but none in PHP/Web development. 我对Android Eclipse有所了解,但在PHP / Web开发方面没有。

Scenario: 场景:

The website is already created. 该网站已经创建。 It works smoothly and everything now. 它现在可以顺利运行。 (It wasnt me who made it though). (虽然我不是这样做的)。 The databases are already uploaded on the host (hostinger.ph). 数据库已经上传到主机(hostinger.ph)。 I just have to connect whatever I do on the eclipse(mobile app) to the databases. 我只需将我在eclipse(移动应用程序)上所做的事情连接到数据库。

What I should do: 我该做什么:

  1. When I login in the mobile application, the mobile app should check if the entered username and password are existing in the database uploaded in the host (hostinger.ph). 当我登录移动应用程序时,移动应用程序应检查输入的用户名和密码是否存在于主机上载的数据库中(hostinger.ph)。
  2. When an admin creates an announcement for the website, it should be uploaded on the database in the host so it shows in the website homepage. 当管理员为网站创建公告时,应将其上载到主机中的数据库中,以便显示在网站主页中。
  3. When an employee files for "Vacation/Sick/Emergency/Maternal/Paternal/any kind of... Leaves", it should be added in the database in the host so the Admin can view it and approve it using the mobile app. 当员工提交“假期/生病/紧急/孕产妇/父亲/任何类型......离开”时,应将其添加到主机的数据库中,以便管理员可以使用移动应用程序查看并批准它。
  4. Any basic viewing of files uploaded on the host. 对主机上传文件的任何基本查看。
  5. Account management (change password, user profile, etc.) 帐户管理(更改密码,用户档案等)

    I'm sorry that I don't have a code since I don't know how to start yet. 很抱歉我没有代码,因为我不知道如何开始。 I only have the login screen right now but I dont know where and how to transfer the username and password to the database. 我现在只有登录屏幕,但我不知道在何处以及如何将用户名和密码传输到数据库。

My Questions: 我的问题:

  1. Can I do it with just Android Eclipse and sqlite? 我可以用Android Eclipse和sqlite做到吗? Comparing my input of user and pass and do something like SELECT * FROM username/password table where the user and pass should match? 比较我的用户输入和传递,并执行SELECT * FROM用户名/密码表,其中用户和传递应匹配?
  2. Can i just do the same when I want to view files? 我想查看文件时可以这样做吗? Sample I click "View Leaves", the app will then do an sql query to retrieve the file? 示例我单击“查看叶子”,然后应用程序将执行sql查询以检索文件?
  3. Same idea i have in account management and when an admin creates an update? 我在账户管理和管理员创建更新时的想法相同吗?

I just want to know what else do i need aside from Eclipse, sqlite. 我只是想知道除了Eclipse,sqlite之外我还需要什么。 I was told I need to do a PHP Application, upload it in the host (hostinger.ph) as a backend app. 我被告知我需要做一个PHP应用程序,将其作为后端应用程序上传到主机(hostinger.ph)。 Is this correct? 这个对吗? So I can start on the right track. 所以我可以开始走上正轨。

Thank you so much everybody! 非常感谢大家! sorry for the long post. 抱歉这篇长篇文章。

Example how you send params to server 示例如何将params发送到服务器

public class SendToServerActivity extends Activity {

EditText txtName;
EditText txtPrice;
EditText txtDesc;
EditText inputMnoz;
EditText txtCreatedAt;
Button btnSave;
TextView inputEdiServer;
TextView inputEdiUser;
TextView inputEdiDruhid;

String pid;

// Progress Dialog
private ProgressDialog pDialog;

// JSON parser class
JSONParser jsonParser = new JSONParser();


// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCT = "product";
private static final String TAG_PID = "pid";
private static final String TAG_NAME = "name";
private static final String TAG_PRICE = "price";
private static final String TAG_DESCRIPTION = "description";


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sendlayout);



    // save button
    btnSave = (Button) findViewById(R.id.btnSave);


    // save button click event
    btnSave.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // starting background task to update product
            new SendToServer().execute();
        }
    });



}




class SendToServer extends AsyncTask<String, String, String> {


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

    }


    protected String doInBackground(String... args) {

        // getting updated data from EditTexts
        String name = txtName.getText().toString();
        String price = txtPrice.getText().toString();
        String description = txtDesc.getText().toString();
        String mnoz = inputMnoz.getText().toString();


        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair(TAG_PID, pid));
        params.add(new BasicNameValuePair(TAG_NAME, name));
        params.add(new BasicNameValuePair(TAG_PRICE, price));
        params.add(new BasicNameValuePair(TAG_DESCRIPTION, description));

        params.add(new BasicNameValuePair("mnoz", mnoz));

        // sending modified data through http request
        // Notice that update product url accepts POST method
        JSONObject json = jsonParser.makeHttpRequest("http://xxx.xx/xxx/getparams.php",
                "POST", params);

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

            if (success == 1) {
            //save ok

            } else {
                // failed save
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }


    protected void onPostExecute(String file_url) {

    }
}

}

and php script getparams.php on the server 和PHP脚本getparams.php在服务器上

<?php


// array for JSON response
$response = array();

// check for required fields
if (isset($_POST['pid']) && isset($_POST['name']) && isset($_POST['price']) && isset($_POST['description'])) {

$pid = $_POST['pid'];
$name = $_POST['name'];
$price = $_POST['price'];
$description = $_POST['description'];



// connecting to db
...
...


//some mysql operation
$resttt = "INSERT INTO sometable ( pid, name, price, description ) VALUES ( '$pid', '$name', '$price', '$description'  ) ";
$result = mysql_query("$resttt") or die(mysql_error());


// check if row inserted or not
if ($result) {
    // successfully updated
    $response["success"] = 1;
    $response["message"] = "Product successfully saved.";


    // echoing JSON response
    echo json_encode($response);
} else {

}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";

// echoing JSON response
echo json_encode($response);
}
?>

may be help 可能会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM