connexion applications android de base de données mysql

J'ai essayé le tutoriel indiqué sur divers sites web sur la connexion d'une base de données MySQL à l'aide de php pour android. Je ne sais pas quel est le problème avec mon code ci-dessous. Quelqu'un peut me dire ce que je dois faire.

C'est mon code php

 <?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(); ?>

C'est ma requête 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 
)

C'est mon code java dans android

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<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
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<jArray.length();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();
}
}
}

Le programme de travail de l'amende. mais je ne peux pas se connecter à http://localhost/index.php. L'affichage du programme échoue 3 fois. Pouvez m'aider à voir où j'ai fait une erreur?

Remercier tout le monde pour les aider. Maintenant, je suis en mesure de se connecter à mysql. Mais je ne peux pas obtenir la valeur des données json. Le prog toast un msg 2 passes et 1 échec. Quelqu'un peut-il m'aider? L'image ci-dessous, c'est quand je tape http://localhost/index.php dans mon IE. Et la ligne 6 est tous ce

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

Je ne sais pas où je va mal.

connexion applications android de base de données mysql

  • Je n'y connais rien en PHP, mais je vois que vous avez la bonne réponse sur votre demande: tableau d'objets JSON. Probablement script php fonctionne normalement.
InformationsquelleAutor sugianto | 2011-04-03