Erreur dans le format JSON
Bonsoir tout le monde,
je suis debutante en Android et Java.je travail sur mon memoire .
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
Code:
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| 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:
Code:
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| 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:
Code:
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
| 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:
Code:
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
| <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:
Code:
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
| <?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> |