使用php从mysql到android获取数据

使用php从mysql到android获取数据,php,android,mysql,json,httpclient,Php,Android,Mysql,Json,Httpclient,我目前正在尝试开发一个应用程序,它可以从mysql服务器发送和接收数据 该应用程序调用一个php脚本,该脚本连接到mysql服务器。我已经成功开发了发送部分,现在我想从mysql中检索数据并在android手机上显示 mysql表由5列组成: bssid 建筑 楼层 lon lat php文件getdata.php包含: <?php $con = mysql_connect("localhost","root","xxx"); if(!$con)

我目前正在尝试开发一个应用程序,它可以从mysql服务器发送和接收数据

该应用程序调用一个php脚本,该脚本连接到mysql服务器。我已经成功开发了发送部分,现在我想从mysql中检索数据并在android手机上显示

mysql表由5列组成:

  • bssid
  • 建筑
  • 楼层
  • lon
  • lat
php文件
getdata.php
包含:

     <?php
     $con = mysql_connect("localhost","root","xxx");
     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';
     }
     $sql = mysql_query("SELECT building,floor,lon,lat FROM ap_location WHERE bssid='00:19:07:8e:f7:b0'");

     while($row=mysql_fetch_assoc($sql))
     $output[]=$row;
     print(json_encode($output));

      mysql_close(); ?>

使用HttpGet而不是HttpPost并解析您的url。

这里有一个我经常用来获取的类

public JSONObject get(String urlString){       
    URL currentUrl;
    try {
        currentUrl = new URL(currentUrlString);
    } catch (MalformedURLException e) {
        e.printStackTrace();
        return null;
    }

    HttpURLConnection urlConnection = null;
    InputStream in;
    BufferedReader streamReader = null;
    StringBuilder responseStrBuilder = new StringBuilder();
    String inputStr;
    try {
        urlConnection = (HttpURLConnection) currentUrl.openConnection();            
        in = new BufferedInputStream(urlConnection.getInputStream());
        streamReader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
        while ((inputStr = streamReader.readLine()) != null) {
            responseStrBuilder.append(inputStr);
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        urlConnection.disconnect();
        if(null != streamReader){
            try {
                streamReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    try {
        return new JSONObject(responseStrBuilder.toString());
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}

尝试使用
get(“http://echo.jsontest.com/key/value");

碰撞。我需要进一步解释我的问题吗?我测试了代码,它对我来说运行良好。您是否已将此权限添加到清单文件中
@freddy您能帮我获取整列值并存储在一个数组中吗,谢谢
public JSONObject get(String urlString){       
    URL currentUrl;
    try {
        currentUrl = new URL(currentUrlString);
    } catch (MalformedURLException e) {
        e.printStackTrace();
        return null;
    }

    HttpURLConnection urlConnection = null;
    InputStream in;
    BufferedReader streamReader = null;
    StringBuilder responseStrBuilder = new StringBuilder();
    String inputStr;
    try {
        urlConnection = (HttpURLConnection) currentUrl.openConnection();            
        in = new BufferedInputStream(urlConnection.getInputStream());
        streamReader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
        while ((inputStr = streamReader.readLine()) != null) {
            responseStrBuilder.append(inputStr);
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        urlConnection.disconnect();
        if(null != streamReader){
            try {
                streamReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    try {
        return new JSONObject(responseStrBuilder.toString());
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}