从机器人将数据发送到网站和接收网络 [英] Sending data from android to website and receiving on web

查看:112
本文介绍了从机器人将数据发送到网站和接收网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新android开发和, 我想开发的Andr​​oid应用程序,将数据发送到我的网站,(例如经度和放大器;纬度),我的问题是如何从Android的发送数据,以及如何获取/捕获它的网站

I am new to android development and, I want to develop an application for android, that will send data to my website, (for example longitude & latitude), my question is how to send data from android and how to get/capture it on website.

推荐答案

这可能会有帮助。只是一个例子,我做了前一段时间的加速度发送数据到服务器,所以我可以将数据存储到一个MySQL数据库。 为了将数据发送到服务器,你可以这样做:

This might help. Just an example I did a while ago sending acceleration data to server so I could store the data into a mysql database. To send data to server you could do this:

private void sendData(ArrayList<NameValuePair> data)
{
     // 1) Connect via HTTP. 2) Encode data. 3) Send data.
    try
    {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new      
        HttpPost("http://www.blah.com/AddAccelerationData.php");
        httppost.setEntity(new UrlEncodedFormEntity(data));
        HttpResponse response = httpclient.execute(httppost);
        Log.i("postData", response.getStatusLine().toString());
            //Could do something better with response.
    }
    catch(Exception e)
    {
        Log.e("log_tag", "Error:  "+e.toString());
    }  
}

然后派可以说:

then to send lets say:

private void sendAccelerationData(String userIDArg, String dateArg, String timeArg,
        String timeStamp, String accelX, String accelY, String accelZ)
{
    fileName = "AddAccelerationData.php";

    //Add data to be send.
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(7);
    nameValuePairs.add(new BasicNameValuePair("userID", userIDArg));
    nameValuePairs.add(new BasicNameValuePair("date",dateArg));
    nameValuePairs.add(new BasicNameValuePair("time",timeArg));
    nameValuePairs.add(new BasicNameValuePair("timeStamp",timeStamp));

    nameValuePairs.add(new BasicNameValuePair("accelX",accelX));
    nameValuePairs.add(new BasicNameValuePair("accelY",accelY));
    nameValuePairs.add(new BasicNameValuePair("accelZ",accelZ));

    this.sendData(nameValuePairs);
}

所以后来在服务器上的AddAccelerationData.php文件是:

so then the AddAccelerationData.php file on server is:

<?php
/*
 * What this file does is it:
 * 1) Creates connection to database.
 * 2) Retrieve the data being send.
 * 3) Add the retrieved data to database.
 * 4) Close database connection.
 */
require_once '../Connection.php'; //connect to a database/disconnect handler.
require_once '../SendAPI.php'; //deals with sending querys.

$server = new Connection();
$send = new Send();

//Connect to database.
$server->connectDB();

//Retrieve the data.
$userID = $_POST['userID'];
$date = $_POST['date'];
$time = $_POST['time'];

$accelX = $_POST['accelX'];
$accelY = $_POST['accelY'];
$accelZ = $_POST['accelZ'];

//Add data to database. //Personal method to query and add to database.
$send->sendAccelerationData($userID, $date, $time, $timeStamp, $accelX, $accelY, $accelZ);


//Disconnect from database.
$server->disconnectDB();
?>

这是我最近使用过的一个例子。只是要注意,在PHP文件。我导入Connection.php 这只是涉及的是连接到数据库。因此,只需更换,以你的code,用于连接到MySQL数据库。此外,我进口SendAPI.php(可以忽略),这只是我的类用于发送数据。基本上,它包含了一些我想用querys的。如sendAccelerationData()。基本上类是类似于存储过程。

This is an example I used recently. Just to note in the php file. I import Connection.php this just deals with the connection to the database. So just replace that with your code for connecting to MYSQL db. Also I imported SendAPI.php (which you can just ignore)This was just my class for sending data. Basically it contained some of the querys I wanted to use. Such as sendAccelerationData(). Basically class was similar to that of stored procedures.

这篇关于从机器人将数据发送到网站和接收网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆