将整数和文本字符串等数据从手机发送到网络数据库

2022-01-05 00:00:00 php mysql apache phpmyadmin android

我有一个项目,我应该将整数、浮点数和文本字符串等数据从 Android 应用程序发送到网络数据库.但是,我没有关于如何执行此操作的第一个线索.有人可以对此有所了解吗???任何建议或帮助将不胜感激.

I have a project where I'm supposed to send data such as integers, floats and text strings from an android app to a web database. However I don't have the first clue on how to do this. Could somebody shed some light on this please??? Any advise or help would be much appreciated.

推荐答案

  1. 您需要编写一些服务器端逻辑(通过 POST 或 GET 方法接受参数 key=value 的 PHP 页面)
  2. 然后,如果数据经过验证,则将其保存到数据库中
  3. 在手机中,您必须实现 HttpClient 和 HttpPost 类才能将此数据发布到 PHP 页面

在手机中,您可以使用以下代码(未测试):

In phone you can use following code (no tested):

    public void postData() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("id", "12345"));
        nameValuePairs.add(new BasicNameValuePair("stringdata", "Hi"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
} 

在 PHP 中你可以这样做:

In PHP you can do something like this:

<?php
//Check whether the data has been submitted
if (isset($_POST['id'] && isset($_POST['stringdata'])) ) {

   //Let's now print out the received values in the browser
   echo "Id: {$_POST['id']}<br />";
   echo "String data: {$_POST['stringdata']}<br />";

   //you can implement database logic here too (insert data to database)
} else {
    echo "You can't see this page without submitting the data.";
}
?>

相关文章