使用php从mysql到android获取数据 [英] Get data from mysql to android with php

查看:108
本文介绍了使用php从mysql到android获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试开发一个应用程序,该应用程序除其他功能外还可以从mysql服务器发送和接收数据.

I am currently trying to develop an app that among other things can send and receive data from a mysql server.

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

The app calls a php script which makes the connection to the mysql server. I have successfully developed the sending part and now I want to retrieve data from mysql and display it on an android phone.

mysql表由5列组成:

The mysql table consists of 5 columns:

  • bssid
  • building
  • floor
  • lon
  • lat
  • bssid
  • building
  • floor
  • 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(); ?>

在浏览器中测试后,此部分工作正常.

This part is working fine, when tested in a browser.

用于连接到php的Java代码:

The java code for connecting to php:

    public class Database {
        public static Object[] getData(){
    String db_url = "http://xx.xx.xx.xx/getdata.php";
    InputStream is = null;
    String line = null;
    ArrayList<NameValuePair> request = new ArrayList<NameValuePair>();
    request.add(new BasicNameValuePair("bssid",bssid));
    Object returnValue[] = new Object[4];
    try
    {
        HttpClient httpclient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpPost httppost = new HttpPost(db_url);
        httppost.setEntity(new UrlEncodedFormEntity(request));
        HttpResponse response = httpclient.execute(httppost, localContext);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    }catch(Exception e){
        Log.e("log_tag", "Error in http connection" +e.toString());
    }
    String result = "";
    try
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();

        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        result=sb.toString();
    }catch(Exception e){
        Log.e("log_tag", "Error in http connection" +e.toString());
    }
    try
    {
        JSONArray jArray = new JSONArray(result);
        JSONObject json_data = jArray.getJSONObject(0);
        returnValue[0] = (json_data.getString("building"));
        returnValue[1] = (json_data.getString("floor"));
        returnValue[2] = (json_data.getString("lon"));
        returnValue[3] = (json_data.getString("lat"));

    }catch(JSONException e){
        Log.e("log_tag", "Error parsing data" +e.toString());
    }
    return returnValue;
}

}

这是修改后的代码,用于将数据发送到mysql服务器,但是出了点问题. 我尝试通过在代码中设置不同的returnValue进行测试,这表明具有httpclient连接的部件未运行.

This is a modified code used to send data to the mysql server, but something is wrong. I've tried to test it by setting different returnValues in the code and this shows me that the part with the httpclient connection does not run.

你们能帮我吗?

我希望这不太令人困惑,如果您愿意,我可以尝试进一步解释.

I hope this is not too confussing, and if you want I can try to explain it futher.

推荐答案

使用HttpGet代替HttpPost并解析您的网址.

Use HttpGet instead of HttpPost and parse your url.

这篇关于使用php从mysql到android获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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