【问题标题】:Android Mobile Application Connection to a Web Host DatabaseAndroid 移动应用程序连接到 Web 主机数据库
【发布时间】:2015-04-16 04:40:17
【问题描述】:
我目前正在为一个网站创建一个移动应用程序,我必须在其中连接到在网络主机上上传的数据库。我有几个关于我应该如何开始的问题。我有 Android Eclipse 方面的知识,但没有 PHP/Web 开发方面的知识。
场景:
网站已创建。它工作顺利,现在一切正常。 (虽然不是我做的)。数据库已上传到主机 (hostinger.ph)。我只需要将我在 eclipse(移动应用程序)上所做的任何事情都连接到数据库。
我应该做什么:
- 当我登录移动应用程序时,移动应用程序应检查输入的用户名和密码是否存在于主机上传的数据库 (hostinger.ph) 中。
- 当管理员为网站创建公告时,应将其上传到主机中的数据库中,以便显示在网站主页中。
- 当员工提交“假期/病假/紧急情况/产妇/父亲/任何类型的......离开”时,应将其添加到主机的数据库中,以便管理员可以使用移动设备查看和批准它应用程序。
- 对主机上上传的文件的任何基本查看。
-
帐户管理(更改密码、用户资料等)
很抱歉我没有代码,因为我还不知道如何开始。我现在只有登录屏幕,但我不知道在哪里以及如何将用户名和密码传输到数据库。
我的问题:
- 我可以只使用 Android Eclipse 和 sqlite 吗?比较我输入的用户和密码,然后执行 SELECT * FROM 用户名/密码表之类的操作,用户和密码应该匹配?
- 当我想查看文件时,我可以这样做吗?示例 我点击“查看叶子”,应用程序会执行 sql 查询来检索文件?
- 我在帐户管理和管理员创建更新时的想法相同?
我只想知道除了 Eclipse、sqlite 之外我还需要什么。有人告诉我我需要做一个 PHP 应用程序,将它作为后端应用程序上传到主机 (hostinger.ph)。它是否正确?所以我可以从正确的轨道开始。
非常感谢大家!很抱歉发了这么长的帖子。
【问题讨论】:
标签:
php
android
mysql
eclipse
sqlite
【解决方案1】:
示例如何将参数发送到服务器
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);
}
?>
可能有帮助