Partage
  • Partager sur Facebook
  • Partager sur Twitter

Recuperation des données dans la base des données

erreur dans le format Json

    18 juin 2015 à 21:28:11

    depuis plus d'une semaine,je n'arrive  pas à récupere mes données dans une liste.

    j'ai reussi à etablir la connexion avec Mysql,d'ou j'ai recupere les données dans le logcat,mais dans la liste je n'y arrive pas

    s'il vous plait aider moi à ressoudre le problème.

    Merci

    voici mon code:

    1)code java

    a)ConnexionBDD.java

    package com.example.foodoran;

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

    // listeRestaurants=new ArrayList<Restaurant>();

    restaurant.setNom(jArray.getJSONObject(i).getString("Nom").toString()); 

    restaurant.setRue(jArray.getJSONObject(i).getString("Specialité").toString());

    //on met les données dans la liste

    listeRestaurants.add(restaurant);

    }

    }catch(JSONException jex){

    jex.printStackTrace();

    }

    //return stringB;

     return listeRestaurants;

    }}

    b) AdapterResto.java:

    package com.example.foodoran;

    import java.util.ArrayList;

    import android.content.Context;

    import android.view.LayoutInflater;

    import android.view.View;

    import android.view.ViewGroup;

    import android.widget.BaseAdapter;

    import android.widget.TableLayout;

    import android.widget.TextView;

    public class AdapterResto extends BaseAdapter {

    private Context context;    //l'activité dans laquelle on sera lorsqu'on aura besoin d'appeler l'adaptater

    private LayoutInflater OInflater; //permet de lier les données avec le Layout que nous avons créer

        private ArrayList<Restaurant >listeRestaurants;

        public  AdapterResto(Context pcontext, ArrayList<Restaurant >plisteRestauarnt){

       this.context=pcontext;

       this.listeRestaurants=plisteRestauarnt;

       this.OInflater=LayoutInflater.from(this.context);

        }

    @Override

    public int getCount(){ 

    return this.listeRestaurants.size();

    }

    @Override

    public Object getItem(int pPosition) {// recuperer l'item à une position bien definie à l'interieur de la liste

    return this.listeRestaurants.get(pPosition);

    }

    @Override

    public long getItemId(int pPosition) {

    return pPosition ;

    }

    @SuppressWarnings("unused")

    @Override

    public View getView(int pPosition, View pConvertView, ViewGroup pParent) {

    TableLayout tLayoutItem; 

    if(pConvertView==null){

    tLayoutItem=(TableLayout)OInflater.inflate(R.layout.liste_recherche,pParent, false);//attache le données à patir de tableLayout

    }else{

    tLayoutItem=(TableLayout)pConvertView;

    }//on recuperer les données dans la BDD

    TextView textRestaurant=(TextView)tLayoutItem.findViewById(R.id.resto);

    textRestaurant.setText(this.listeRestaurants.get(pPosition).getNom().toString().trim());

    TextView textSpe=(TextView)tLayoutItem.findViewById(R.id.sp);

    textRestaurant.setText(this.listeRestaurants.get(pPosition).getSpecialité().toString().trim());

    return tLayoutItem;

    }

    c)ListeRestaurant.java:
    package com.example.foodoran;
    import java.util.ArrayList;
    import java.util.concurrent.ExecutionException;
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.ListView;
    public class ListeRestaurant extends  Activity {
    Restaurant restaurant;
    ArrayList<Restaurant >listeRestaurants;
    @SuppressWarnings("unchecked")
    protected void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main_liste_restaurant);
            ConnexionBDD connecteur = new ConnexionBDD ();
    connecteur.execute();
    ArrayList<Restaurant> listeRestautants = null;
    try {
    //StringBuffer sb = (StringBuffer) connecteur.get();
    listeRestautants = (ArrayList<Restaurant>) connecteur.get();
    System.out.println("LISTE RESTAURANTS : " + listeRestautants);
    } catch (InterruptedException e) {
    //e.printStackTrace();
    System.err.println(e);
    } catch (ExecutionException e) {
    //e.printStackTrace();
    System.err.println(e);
    }
    if(listeRestaurants != null){
       AdapterResto adapter = new AdapterResto(this,listeRestaurants );
       ListView listeViewResto=(ListView) findViewById(R.id.listRestaurant); 
       listeViewResto.setAdapter(adapter);
       }
    }
    }

    2)code xml:

    a)liste_recherche.xml:

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"

        xmlns:tools="http://schemas.android.com/tools"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent" >

        <TableRow

            android:id="@+id/tableRow1"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            tools:ignore="UselessParent" >

            <TextView

                android:id="@+id/resto"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_gravity="left"

                android:layout_column="0"

                android:textSize="20sp"

                android:textStyle="italic"

                tools:ignore="RtlHardcoded"

                android:visibility="visible" />

            <TextView

                android:id="@+id/sp"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:textSize="15sp"

                android:layout_column="1"

                android:textStyle="italic"

                android:visibility="visible" />

        </TableRow>

    </TableLayout>

    b)main.xml:

    <?xml version="1.0" encoding="utf-8"?>

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:background="@color/rouge"

         >

        <TextView

            android:id="@+id/textView1"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_alignParentTop="true"

            android:layout_centerHorizontal="true"

            android:text="@string/oran" />

        <include

            android:id="@+id/lstRestaurant"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            layout="@layout/liste_recherche" />

    </RelativeLayout>

    • Partager sur Facebook
    • Partager sur Twitter
      18 juin 2015 à 21:43:22

      S'il vous plait Excusez moi,

      bonsoir tout le monde

      • Partager sur Facebook
      • Partager sur Twitter

      Recuperation des données dans la base des données

      × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
      × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
      • Editeur
      • Markdown