1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
package com.oranfood;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.widget.ListView;
public class ConnexionBDD extends AsyncTask <Object, Object, Object>{
@Override
protected Object doInBackground(Object... params) {
Restaurant restaurant;
//ArrayList<Restaurant >listeRestaurants;
ArrayList<Restaurant> listeRestaurants =new ArrayList<Restaurant>();
StringBuffer stringB=new StringBuffer("");
BufferedReader bufr=null;
try{
HttpClient client=new DefaultHttpClient();
HttpPost requete = new HttpPost("http://192.168.56.1/requeteAndroid/requete.php");
HttpResponse reponse=client.execute(requete);
System.out.println("CONNEXION REUSSIE !!");
InputStream is= reponse.getEntity().getContent();
bufr=new BufferedReader(new InputStreamReader(is));
//lire le contenu des données recuperent
String ligneLue=bufr.readLine();
while (ligneLue!= null){
stringB.append(ligneLue);
stringB.append("\n");
ligneLue=bufr.readLine();
}
}catch(Exception e){
// e.printStackTrace();
System.err.println("PAS DE CONNEXION : " + e);
}
try{
JSONArray jArray=new JSONArray(stringB.toString());
for(int i=0; i<jArray.length();i++){
restaurant=new Restaurant();
restaurant.setNom(jArray.getJSONObject(i).getString("Nom").toString());
restaurant.setSpecialité(jArray.getJSONObject(i).getString("Specialité").toString());
//on met les données dans la liste
listeRestaurants.add(restaurant);
}
}catch(JSONException jex){
jex.printStackTrace();
}
return listeRestaurants;
}} |
Partager