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

发布于 2024-10-25 17:04:13 字数 98 浏览 0 评论 0原文

我有一个项目,我应该将整数、浮点数和文本字符串等数据从 Android 应用程序发送到 Web 数据库。但是我不知道如何做到这一点。有人可以解释一下吗???任何建议或帮助将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

做个ˇ局外人 2024-11-01 17:04:14
  1. 您需要编写一些服务器端逻辑(通过 POST 或 GET 方法接受参数 key=value 的 PHP 页面)
  2. 然后,如果数据经过验证,则将其保存到数据库
  3. 在手机中,您必须实现 HttpClient 和 HttpPost 类以将此数据 POST 到 PHP

页面手机你可以使用以下代码(未经测试):

    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 中你可以执行如下操作:

<?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.";
}
?>
  1. You need program some server-side logic (PHP page that accepting parameters key=value by POST or GET method)
  2. Then if data is verified save it to the database
  3. In phone you must implement HttpClient and HttpPost classes to POST this data to PHP page

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
    }
} 

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.";
}
?>
迷爱 2024-11-01 17:04:14

看看肥皂
(在 Android 上您可以使用 ksoap2 包)
您还可以在服务器端创建一个到 o 程序的套接字连接

Take a look on soap
(on android you can use the ksoap2 package)
you cen also create a socket connection to o program on the server side

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文