Get data from mysql in android using php

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

The application calls a php script that establishes a connection to the mysql server. I have successfully developed the starting part, and now I want to get data from mysql and display it on an Android phone.

The mysql table consists of 5 columns:

  • bssid
  • building
  • floor
  • lon
  • lat

The php file getdata.phpcontains:

     <?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 works fine when tested in a browser.

Java code to connect 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;
}

}

This is a modified code used to send data to mysql server, but something is wrong. I tried to test it by setting different ones in the code returnValue, and this shows me that the part with the connection httpclientdoes not start.

, , ?

, , , .

+5
2

HttpGet HttpPost URL.

+1

, GET

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");

0

All Articles