php 查询mysql android_使用php从mysql获取数据到android

我目前正在尝试开发一个应用程序,其中包括可以从

mysql服务器发送和接收数据.

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

mysql表由5列组成:

> bssid

>建设

>地板

> lon

>拉特

php文件getdata.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 request = new ArrayList();

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连接的部分不会运行.

你们能帮助我吗?

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值