我正在开发一个 android 应用程序,其想法是当您按下按钮时,该应用程序将连接到数据库并显示数据库内容。这里的问题是什么都没有发生。
这是 onclick() 方法
Button butt1=(Button)findViewById(R.id.buttonUN);
butt1.setOnClickListener( new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
"chargement en cours", Toast.LENGTH_LONG).show();
StringBuilder responseHTTP = new StringBuilder();//stocker la reponse HTTP
HttpClient client =new DefaultHttpClient(); //envoi de requete HTTP
HttpGet httpGet =new HttpGet //recuperer l'url de fichier PHP
("http://192.168.12.5/BaseUne/connect.php"); // via la methode HttpGet
try{
HttpResponse response= client.execute(httpGet); // recevoir la reponse
StatusLine statusline=response.getStatusLine(); // StatusLine: methode pour savoir le statut de
int statuscode = statusline.getStatusCode(); // de la connexion
if (statuscode==200){
HttpEntity entity =response.getEntity();
InputStream content = entity.getContent(); //InputStream :recuperer les données binaires
//InputStreamReader: construction des caracteres
BufferedReader reader= new BufferedReader(new InputStreamReader(content));
String line;
while((line=reader.readLine())!=null){
responseHTTP.append(line);
}
JSONObject jsonObject =new JSONObject(responseHTTP.toString()); // creation d'un objet JSON
int ID =jsonObject.getInt("ID_contenu");
String Descrip =jsonObject.getString("Description");
Intent a =new Intent(ChoixDuSite.this,AdminInterface.class);
a.putExtra("String", Descrip);
a.putExtra("int", ID);
startActivity(a);
}
} catch (Exception e){
Toast.makeText(getApplicationContext(),
e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
});
这是我的php文件:
<?php
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("DBname") or die(mysql_error());
$sql=mysql_query("SELECT * FROM TABLEname");
while($row=mysql_fetch_assoc($sql))
print(json_encode($row));
mysql_close();
?>