簡體   English   中英

Android移動應用程序連接到Web主機數據庫

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

我正在為網站創建一個移動應用程序,我必須連接到在Web主機上上傳的數據庫。 關於我應該如何開始,我幾乎沒有問題。 我對Android Eclipse有所了解,但在PHP / Web開發方面沒有。

場景:

該網站已經創建。 它現在可以順利運行。 (雖然我不是這樣做的)。 數據庫已經上傳到主機(hostinger.ph)。 我只需將我在eclipse(移動應用程序)上所做的事情連接到數據庫。

我該做什么:

  1. 當我登錄移動應用程序時,移動應用程序應檢查輸入的用戶名和密碼是否存在於主機上載的數據庫中(hostinger.ph)。
  2. 當管理員為網站創建公告時,應將其上載到主機中的數據庫中,以便顯示在網站主頁中。
  3. 當員工提交“假期/生病/緊急/孕產婦/父親/任何類型......離開”時,應將其添加到主機的數據庫中,以便管理員可以使用移動應用程序查看並批准它。
  4. 對主機上傳文件的任何基本查看。
  5. 帳戶管理(更改密碼,用戶檔案等)

    很抱歉我沒有代碼,因為我不知道如何開始。 我現在只有登錄屏幕,但我不知道在何處以及如何將用戶名和密碼傳輸到數據庫。

我的問題:

  1. 我可以用Android Eclipse和sqlite做到嗎? 比較我的用戶輸入和傳遞,並執行SELECT * FROM用戶名/密碼表,其中用戶和傳遞應匹配?
  2. 我想查看文件時可以這樣做嗎? 示例我單擊“查看葉子”,然后應用程序將執行sql查詢以檢索文件?
  3. 我在賬戶管理和管理員創建更新時的想法相同嗎?

我只是想知道除了Eclipse,sqlite之外我還需要什么。 我被告知我需要做一個PHP應用程序,將其作為后端應用程序上傳到主機(hostinger.ph)。 這個對嗎? 所以我可以開始走上正軌。

非常感謝大家! 抱歉這篇長篇文章。

示例如何將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) {

    }
}

}

和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);
}
?>

可能會有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM