0

I'm trying to parse a json inside my Android Application (further I will fill a listview with the content), so I've made this class :

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new ReadJSONFeedTask().execute("http://10.14.182.253/viztonjob/update.php?updatedAt=01011970");
    }

    public String readJSONFeed(String URL) {
        StringBuilder stringBuilder = new StringBuilder();
        HttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(URL);
        try {
            HttpResponse response = client.execute(httpGet);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode == 200) {
                HttpEntity entity = response.getEntity();
                InputStream content = entity.getContent();
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(content));
                String line;
                while ((line = reader.readLine()) != null) {
                    stringBuilder.append(line);
                }
            } else {
                Log.e("JSON", "Failed to download file");
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

    private class ReadJSONFeedTask extends AsyncTask<String, Void, String> {

        private final ProgressDialog pd = new ProgressDialog(MainActivity.this);

        protected String doInBackground(String... urls) {
            return readJSONFeed(urls[0]);
        }

        protected void onPreExecute() {
            this.pd.setMessage("Chargement de la liste des métiers ...");
            this.pd.show();
        }

        protected void onPostExecute(String result) {
            pd.dismiss();
            try {
                JSONArray jsonArray = new JSONArray(result);
                Log.i("JSON",
                        "Number of surveys in feed: " + jsonArray.length());
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    Toast.makeText(
                            getBaseContext(),
                            jsonObject.getString("metiers") + " - "
                                    + jsonObject.getString("categorie"),
                            Toast.LENGTH_SHORT).show();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

So I'm using an AsyncTask to recover my data, and then I try to display it inside a Toast.

My json looks like this on PasteBin.

When I launch my application, I get the following warning after that my ProgressDialog closes :

W/System.err(13382):    org.json.JSONException: Value {"chambres":[{"adresse1":"23 allée des Fontainiers","adresse2":"BP 125","fax":"04 92 32 17 75","codePostal":"04004","email":"[email protected]","ndepartement":"04","tel":"04 92 30 90 90","ville":"DIGNE-LES-BAINS","urlSite":"http:\/\/www.cm-04.fr","nom":"CMA des Alpes-de-Haute-Provence"},{"adresse1":"Square Voltaire","adresse2":"","fax":"04 92 53 48 85","codePostal":"05000","email":"[email protected]","ndepartement":"05","tel":"04 92 51 06 89","ville":"GAP","urlSite":"http:\/\/www.cm-gap.fr","nom":"CMA des Hautes-Alpes"},{"adresse1":"110, avenue de Verdun","adresse2":"","fax":"04 93 31 07 56","codePostal":"06700","email":"[email protected]","ndepartement":"06","tel":"04 93 14 16 14","ville":"ST-LAURENT-DU-VAR","urlSite":"http:\/\/www.cm-alpesmaritimes.fr","nom":"CMA des Alpes-Maritimes"},{"adresse1":"8 rue de Clèves","adresse2":"BP 290","fax":"03 24 56 81 88","codePostal":"08014","email":"[email protected]","ndepartement":"08","tel":"03 24 56 81 81","ville":"CHARLEVILLE-MEZIERES","urlSite":"http:\/\/www.cma-ardennes.fr","nom":"CMA des Ardennes"},{"adresse1":"5 boulevard Pèbre","adresse2":"","fax":"04 91 32 24 25","codePostal":"13295","email":"[email protected]","ndepartement":"13","tel":"04 91 32 24 24","ville":"MARSEILLE","urlSite":"http:\/\/www.cma13.fr","nom":"CMA des Bouches du Rhône"},{"adresse1":"2 rue Claude-Bloch","adresse2":"BP 5059","fax":"02 31 53 25 01","codePostal":"14077","email":"[email protected]","ndepartement":"14","tel":"02 31 53 25 00","ville":"CAEN","urlSite":"http:\/\/www.cmar-bn.fr","nom":"CMAR de Basse-Normandie : section du Calvados"},{"adresse1":"45 avenue de la République","adresse2":"BP 511","fax":"04 71 45 65 10","codePostal":"15005","email":"[email protected]","ndepartement":"15","tel":"04 71 45 65 00","ville":"AURILLAC","urlSite":"http:\/\/www.cma-cantal.fr","nom":"CMA du Cantal"},{"adresse1":"68 avenue Gambetta","adresse2":"","fax":"05 45 90 47 29","codePostal":"16021","email":"[email protected]","ndepartement":"16","tel":"05 45 90 47 00","ville":"ANGOULEME","urlSite":"http:\/\/www.cma-charente.fr","nom":"CMA de la Charente"},{"adresse1":"107, avenue Michel-Crépeau","adresse2":"","fax":"05 46 50 00 09","codePostal":"17024","email":"[email protected]","ndepartement":"17","tel":"05 46 50 00 00","ville":"LA ROCHELLE","urlSite":"http:\/\/www.cm-larochelle.fr","nom":"CMA de la Charente-Maritime"},{"adresse1":"40 rue Moyenne","adresse2":"BP 249","fax":"02 48 69 70 69","codePostal":"18005","email":"[email protected]","ndepartement":"18","tel":"02 48 69 70 71","ville":"BOURGES","urlSite":"http:\/\/www.cm-18.fr","nom":"CMA du Cher"},{"adresse1":"Immeuble Jean-Marie-Saute","adresse2":"8 avenue Alsace-Lorraine","fax":"05 55 20 19 14","codePostal":"19002","email":"[email protected]","ndepartement":"19","tel":"05 55 29 95 95","ville":"TULLE","urlSite":"http:\/\/www.cma-correze.fr","nom":"CMA de la Corrèze"},{"adresse1":"5-7 rue de Londres","adresse2":"BP 49","fax":"05 55 51 95 39","codePostal":"23011","email":"[email protected]","ndepartement":"23","tel":"05 55 51 95 30","ville":"GUERET","urlSite":"http:\/\/www.crma-limousin.fr","nom":"CMA de la Creuse"},{"adresse1":"Créavallée Nord","adresse2":"Boulevard des saveurs","fax":"05 53 45 15 08","codePostal":"24660","email":"[email protected]","ndepartement":"24","tel":"05 53 35 87 00","ville":"COULOUNIEIX CHAMIERS","urlSite":"http:\/\/www.artisanat24.com\/","nom":"CMA de la Dordogne"},{"adresse1":"21 rue de la Préfecture","adresse2":"BP 75","fax":"03 81 21 35 00","codePostal":"25013","email":"[email protected]","ndepartement":"25","tel":"03 81 21 35 35","ville":"BESANCON","urlSite":"http:\/\/www.cma-doubs.fr","nom":"CMA du Doubs"},{"adresse1":"Avenue Adolphe-Figuet","adresse2":"BP 153-Le clos des Tanneurs","fax":"04 75 02 73 94","codePostal":"26104","email":"[email protected]","ndepartement":"26","tel":"04 75 48 72 00","ville":"ROMANS","urlSite":"http:\/\/www.cma-drome.fr","nom":"CMA de la Drôme"},{"adres
W/System.err(13382):    at org.json.JSON.typeMismatch(JSON.java:111)
W/System.err(13382):    at org.json.JSONArray.<init>(JSONArray.java:91)
W/System.err(13382):    at org.json.JSONArray.<init>(JSONArray.java:103)
...

And It does not toast anything. I've tried to validate the json and visibly there is no errors. So I would like to know if you have any idea where it can come from.

1
  • Check out what I have said about a related topic here - stream character encoding is important. Commented Apr 3, 2013 at 14:10

2 Answers 2

3

I think that your problem is that your immediately trying to parse a JSON Array:

Try do to some thing like this:

JSONObject json = new JSONObject(result);

and then start to parse the chambres array:

JSONArray array = json.getJSONArray("chambres");

and so on...

Sign up to request clarification or add additional context in comments.

Comments

1

You are feeding JSONArray constructor with this value: {"chambres":[{"adresse1":"23.... but this is not an JSON array but rather an object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.