[英]Android Mobile Application Connection to a Web Host Database
我正在為網站創建一個移動應用程序,我必須連接到在Web主機上上傳的數據庫。 關於我應該如何開始,我幾乎沒有問題。 我對Android Eclipse有所了解,但在PHP / Web開發方面沒有。
場景:
該網站已經創建。 它現在可以順利運行。 (雖然我不是這樣做的)。 數據庫已經上傳到主機(hostinger.ph)。 我只需將我在eclipse(移動應用程序)上所做的事情連接到數據庫。
我該做什么:
帳戶管理(更改密碼,用戶檔案等)
很抱歉我沒有代碼,因為我不知道如何開始。 我現在只有登錄屏幕,但我不知道在何處以及如何將用戶名和密碼傳輸到數據庫。
我的問題:
我只是想知道除了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.