如何将安卓连接php的mysql,php – 将android应用程序连接到mysql数据库

我一直在尝试在各种网站上显示的教程,使用php连接MySQL数据库到android.我不知道下面的代码有什么问题.谁能告诉我我需要做什么.

这是我的PHP代码

mysql_connect("localhost","root","sugi");

mysql_select_db("android");

$q=mysql_query("SELECT * FROM people

WHERE

birthyear>'".$_REQUEST['year']."'");

while($e=mysql_fetch_assoc($q))

$output[]=$e;

print(json_encode($output));

mysql_close(); ?>

这是我的SQL查询

CREATE TABLE `people` (

`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,

`name` VARCHAR( 100 ) NOT NULL ,

`sex` BOOL NOT NULL DEFAULT '1',

`birthyear` INT NOT NULL

)

这是我在android中的java代码

public class main extends Activity {

InputStream is;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

String result = "";

//the year data to send

ArrayList nameValuePairs = new ArrayList();

nameValuePairs.add(new BasicNameValuePair("year","1990"));

//http post

try{

HttpClient httpclient = new DefaultHttpClient();

HttpPost httppost = new HttpPost("http://localhost/index.php");

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

HttpResponse response = httpclient.execute(httppost);

HttpEntity entity = response.getEntity();

is = entity.getContent();

Log.e("log_tag", "connection success ");

Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();

}catch(Exception e){

Log.e("log_tag", "Error in http connection "+e.toString());

Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show();

}

//convert response to string

try{

BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);

StringBuilder sb = new StringBuilder();

String line = null;

while ((line = reader.readLine()) != null) {

sb.append(line + "\n");

Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();

}

is.close();

result=sb.toString();

}catch(Exception e){

Log.e("log_tag", "Error converting result "+e.toString());

Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show();

}

//parse json data

try{

JSONArray jArray = new JSONArray(result);

for(int i=0;i

JSONObject json_data = jArray.getJSONObject(i);

Log.i("log_tag","id: "+json_data.getInt("id")+

", name: "+json_data.getString("name")+

", sex: "+json_data.getInt("sex")+

", birthyear: "+json_data.getInt("birthyear")

);

Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();

}

}catch(JSONException e){

Log.e("log_tag", "Error parsing data "+e.toString());

Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show();

}

}

}

该计划工作正常.但我无法连接到http://localhost/index.php.程序显示失败3次.可以帮我看看我哪里出错了?

感谢大家的帮助.现在我能够连接到mysql.但我无法获得json数据的价值.编程传播一个msg 2传递,1个失败.谁能帮我?下图是我在IE中键入http://localhost/index.php的时候.而第6行就是这一切

$q=mysql_query("SELECT * FROM people WHERE birthyear>'".$_REQUEST['year']."'");

我不知道我哪里出错了.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值