将数据从android发送到mysql

时间:2011-11-17 11:04:39

标签: php android mysql

我真的需要一些帮助来完成我的项目。

我有一个Android应用程序,可以扫描无线网络。我想将这些数据上传到mysql数据库。

Android有数据库类:

public class Database {

public static void putServerData(String building, String floor, String room, String address, String signal){
    String db_url = "http://**(IP ADDRESS)**/setdata.php";

    @SuppressWarnings("unused")
    InputStream is = null;
    ArrayList<NameValuePair> request = new ArrayList<NameValuePair>();
    request.add(new BasicNameValuePair("building",building));
    request.add(new BasicNameValuePair("floor",floor));
    request.add(new BasicNameValuePair("room",room));
    request.add(new BasicNameValuePair("address",address));
    request.add(new BasicNameValuePair("signal",signal));


    try
    {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(db_url);
        httppost.setEntity(new UrlEncodedFormEntity(request));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();

    }catch(Exception e){

    }

}

这称为:

            try {
                Database.putServerData(building,floor, room, array1.get(1).toString(), array2.get(2).toString());                   
            } catch (Exception e) {
                Log.e("test", "test");
            }

服务器是Win7机器,带有apache php和mysql

setdata.php文件:

   <?php
   $con = mysql_connect("localhost","root","pass");
   if(!$con)
   {
 echo 'Not connected';
 echo ' - ';

  }else
  {
 echo 'Connection Established';
 echo ' - ';
 }

 $db = mysql_select_db("android");
 if(!$db)
 {
echo 'No database selected';
 }else
 {
echo 'Database selected';
 }



$building = $_POST['building'];
$floor = $_POST['floor'];
$room = $_POST['room'];
$ap_mac1 = $_POST['ap_mac1'];
$ap_strength1 = $_POST['ap_strength1'];
$ap_mac2 = $_POST['ap_mac2'];
$ap_strength2 = $_POST['ap_strength2'];
$ap_mac3 = $_POST['ap_mac3'];
$ap_strength3 = $_POST['ap_strength3'];
$ap_mac4 = $_POST['ap_mac4'];
$ap_strength4 = $_POST['ap_strength4'];
$ap_mac5 = $_POST['ap_mac5'];
$ap_strength5 = $_POST['ap_strength5'];

    echo ($_POST['building']);

mysql_query("INSERT INTO wifiscan VALUES($nextid, '$building','$floor','$room','$ap_mac1','$ap_strength1','$ap_mac2','$ap_strength2', '$ap_mac3', '$ap_strength3', '$ap_mac4', '$ap_strength4', '$ap_mac5', '$ap_strength5')");



mysql_close(); ?>

这不起作用。我不确定我是否以正确的方式选择数据库,这可能是问题吗?

也许这不是那么清楚,如果你愿意的话,病就会进一步解释。

1 个答案:

答案 0 :(得分:1)

使用一些Log和sprintf来确定究竟发生了什么。 我开始使用PHP。尝试从浏览器打开URL并在PHP中放置一些输出,看看这里是否一切正常。发布您的结果,您可能会得到进一步的帮助。