【问题标题】:Get data from mysql to android with php使用php从mysql获取数据到android
【发布时间】:2012-01-22 15:19:25
【问题描述】:

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

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

mysql表由5列组成:

  • 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(); ?>

在浏览器中测试时,这部分工作正常。

连接php的java代码:

    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服务器发送数据,但是有问题。 我试图通过在代码中设置不同的returnValues 来测试它,这表明带有httpclient 连接的部分没有运行。

你们能帮帮我吗?

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

【问题讨论】:

  • 凹凸。我需要进一步解释我的问题吗?
  • 我测试了代码,它对我来说很好。
  • 您是否已将此权限添加到清单文件中? &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  • @freddy 你能帮我获取整个列的值并存储在一个数组中吗,谢谢

标签: php android mysql json httpclient


【解决方案1】:

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

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");进行测试

【讨论】:

    【解决方案2】:

    使用 HttpGet 代替 HttpPost 并解析你的 url。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-22
      • 2011-06-16
      • 2012-12-15
      • 2016-09-06
      • 2020-03-26
      • 2018-03-09
      • 2014-05-07
      • 1970-01-01
      相关资源
      最近更新 更多