Bonjour,

J'affiche une liste d'articles dans une list view que je récupère depuis une base de données à l'aide de JSON. Pour cela, pas de soucis ça fonctionne. Ensuite, lors du clic sur un des articles, je veux afficher le détail de ces articles dans une autre activité. Quand je clique sur le premier article, j'affiche bien les détails souhaités. Mais pas pour les autres, j'ai l'erreur : Error parsing data org.json.JSONException: End of input at character 0 of

Dans les logs, je vois que ça ne prends pas en compte la bonne chaîne JSON, j'ai D/Single*Article*Details: {"articles":[{"idArticle":"2","titre":"test deux","dateArticle":"25-05-2016","heureArticle":"08:59"},{"idArticle":"1","titre":"essai","dateArticle":"19-05-2016","heureArticle":"9:33"}],"success":1} (le JSON que j'utilise pour lister mes articles) au lieu de Single*Article*Details: {"success":1,"article":[{"idArticle":"1","titre":"essai","image":null,"description":"essai test","contenu":"essai test essai test essai test essai test essai test essai test essai test essai test essai test"}]}

J'ai vérifié l'identifiant de l'article passé en extra, et c'est bien le bon qui passe..

Je ne vois pas pourquoi ça ne fonctionne que dans un cas, si quelqu'un peut m'aider je suis preneur !

Merci d'avance

Voici mon code :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
public class LireArticleActivity extends AppCompatActivity {
 
    TextView titre;
    TextView description;
    TextView contenu;
    ImageView image;
 
    String idArticle;
 
    //Progress Dialog
 
    private ProgressDialog pDialog;
 
    // JSON parser class
    JSONParser jsonParser2 = new JSONParser();
 
    // single product url
    private static final String url_one_article = "http://192.168.x.x/GestionArticle/getArticle.php";
 
    // JSON Node names
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_ARTICLE = "article";
    private static final String TAG_ID = "idArticle";
    private static final String TAG_TITRE = "titre";
    private static final String TAG_DESCRIPTION = "description";
    private static final String TAG_CONTENU = "contenu";
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lire_article);
 
       /* titre = (TextView) findViewById(R.id.txtTitre);
        description = (TextView) findViewById(R.id.txtDescription);
        contenu = (TextView) findViewById(R.id.txtContenu);
        image = (ImageView) findViewById(R.id.ivImage) ;*/
 
        // getting product details from intent
        Intent i = getIntent();
 
        // getting product id (pid) from intent
        idArticle = i.getStringExtra(TAG_ID);
        Log.d("id reçu", idArticle);
 
        // Getting complete product details in background thread
        new GetArticleDetail().execute();
 
 
    }
 
 
 
    class GetArticleDetail extends AsyncTask<String, String, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(LireArticleActivity.this);
            pDialog.setMessage("Chargement de l'article..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }
 
        protected String doInBackground(String... params) {
 
            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                    // Check for success tag
                    int success;
                    try {
                        // Building Parameters
                        List<NameValuePair> params = new ArrayList<NameValuePair>();
                        params.add(new BasicNameValuePair("idArticle", idArticle));
                        Log.d("idArticle", idArticle);
 
                        // getting article details by making HTTP request
                        // Note that article details url will use GET request
                        JSONObject json2 = jsonParser2.makeHttpRequest(
                                url_one_article, "GET", params);
 
                        // check your log for json response
                        Log.d("Single Article Details", json2.toString());
 
                        // json success tag
                        success = json2.getInt(TAG_SUCCESS);
 
                        if (success == 1) {
                            // successfully received product details
                            JSONArray articleObj = json2.getJSONArray(TAG_ARTICLE); // JSON Array
 
                            // get first product object from JSON Array
                            JSONObject article = articleObj.getJSONObject(0);
 
                            // article with this id found
                            // Edit Text
                            titre = (TextView) findViewById(R.id.txtTitre);
                            description = (TextView) findViewById(R.id.txtDescription);
                            contenu = (TextView) findViewById(R.id.txtContenu);
                            image = (ImageView) findViewById(R.id.ivImage);
 
                            // display product data in EditText
                            titre.setText(article.getString(TAG_TITRE));
                            description.setText(article.getString(TAG_DESCRIPTION));
                            contenu.setText(article.getString(TAG_CONTENU));
                            //image.
 
                        } else {
                            // product with pid not found
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
 
            return null;
        }
 
        protected void onPostExecute(String file_url) {
            // dismiss the dialog once got all details
            pDialog.dismiss();
        }
 
 
    }
 
}
ainsi que la classe JSONParser :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
import android.util.Log;
 
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;
 
public class JSONParser {
    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";
 
    // constructor
    public JSONParser() {
 
    }
 
    // function get json from url
    // by making HTTP POST or GET mehtod
    public JSONObject makeHttpRequest(String url, String method,
                                      List<NameValuePair> params) {
 
        // Making HTTP request
        try {
 
            // check for request method
            if(method == "POST"){
                // request method is POST
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));
 
                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
 
            }else if(method == "GET"){
                // request method is GET
                DefaultHttpClient httpClient = new DefaultHttpClient();
                String paramString = URLEncodedUtils.format(params, "utf-8");
                url += "?" + paramString;
                HttpGet httpGet = new HttpGet(url);
 
                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
            }
 
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
 
        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");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }
 
        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }
 
        // return JSON String
        return jObj;
 
    }
}
et la page php
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
require_once __DIR__ . '/db_config.php';
$con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE) or die(mysqli_error($con));
 
// check for post data
if (isset($_GET["idArticle"])) {
    $idArticle = $_GET['idArticle'];
 
    // get a article from article table
    $result = mysqli_query($con, "SELECT *FROM article WHERE idArticle = ".$idArticle);
 
    if (!empty($result)) {
        // check for empty result
        if (mysqli_num_rows($result) > 0) {
 
            $result = mysqli_fetch_array($result);
 
            $article = array();
            $article["idArticle"] = $result["idArticle"];
            $article["titre"] = $result["titre"];
            $article["image"] = $result["image"];
			$article["description"] = $result["description"];
            $article["contenu"] = $result["contenu"];
 
            // success
            $response["success"] = 1;
 
            // user node
            $response["article"] = array();
 
            array_push($response["article"], $article);
 
            // echoing JSON response
            echo json_encode($response);
        } else {
            // no article found
            $response["success"] = 0;
            $response["message"] = "No article found";
 
            // echo no users JSON
            echo json_encode($response);
        }
    } else {
        // no product found
        $response["success"] = 0;
        $response["message"] = "No article found";
 
        // echo no users JSON
        echo json_encode($response);
    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";
 
    // echoing JSON response
    echo json_encode($response);
}
?>