开发者

Android app reading a mysql database via php services

I have an application reading a mysql database as shown below

<?php
mysql_connect("host","username","password");
mysql_select_db("Cars");

$q=mysql_query("SELECT * FROM car WHERE model_year>'".$_REQUEST['year']."'");
while($e=mysql_fetch_assoc($q))
        $output[]=$e;

print(json_encode($output));

mysql_close();
?>

and the java android code that send the request and read the data is as shown below

String result = "";
//the year data to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("year","1998"));

//http post
try{
        HttpClient httpclient = new DefaultHtt开发者_运维百科pClient();
        HttpPost httppost = new HttpPost("http://example.com/getmodelsafter.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        InputStream is = entity.getContent();
}catch(Exception e){
        Log.e("log_tag", "Error in http connection "+e.toString());
}

and catches the data after that.

How should be my java code in case I wanted to pass tow different values in my database lets say

<?php
mysql_connect("host","username","password");
mysql_select_db("Cars");

$q=mysql_query("SELECT * FROM car WHERE model_year>'".$_REQUEST['year']."'
 and color="'".$_REQUEST['color']."'");
while($e=mysql_fetch_assoc($q))
        $output[]=$e;

print(json_encode($output));

mysql_close();
?>


I've written a white paper for Embarcadero some months ago. Check it if could be useful for you http://www.embarcadero.com/rad-in-action/php-android

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜