Question

I'm trying to get a JSON response from a server and the object in the url is :

{"nombre": "Christian", "ap_paterno": "Consuelo", "ap_materno": "Mayen",
 "num_telefonico": "5555555", "calle": "calle", "colonia": "colonia", 
 "numero": 43}

I Want to get the "nombre value but I get:

05-15 20:09:51.327: W/System.err(10542): org.json.JSONException: No value for nombre
05-15 20:09:51.327: W/System.err(10542):    at org.json.JSONObject.get(JSONObject.java:355)
05-15 20:09:51.327: W/System.err(10542):    at org.json.JSONObject.getString(JSONObject.java:515)
05-15 20:09:51.327: W/System.err(10542):    at com.example.mibanco.MainActivity$obtieneJSON.doInBackground(MainActivity.java:86)
05-15 20:09:51.327: W/System.err(10542):    at com.example.mibanco.MainActivity$obtieneJSON.doInBackground(MainActivity.java:1)
05-15 20:09:51.327: W/System.err(10542):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
05-15 20:09:51.327: W/System.err(10542):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
05-15 20:09:51.327: W/System.err(10542):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
05-15 20:09:51.327: W/System.err(10542):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
05-15 20:09:51.327: W/System.err(10542):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
05-15 20:09:51.327: W/System.err(10542):    at java.lang.Thread.run(Thread.java:841)

My code is that:

   class obtieneJSON extends AsyncTask<Boolean, Boolean, Boolean> {

        @Override
        protected Boolean doInBackground(Boolean... params) {

            JSONParser jsonparser = new JSONParser();

            JSONObject json = jsonparser.getJSONFromUrl("http://192.168.1.68:8000/api/clientes/1/?format=json");

            try {
                texto = json.getString("nombre");
                System.out.println(texto);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            runOnUiThread(new Runnable() {
                public void run() {
                    TextView et = (TextView)findViewById(R.id.ETInformacion);
                    et.setText(texto);
                }
            });

            return null;
        }
    }

I suppose that the exception is sended in the try/catch block, but at the end the problem is not solver even when I use another attribute of the JSON object.

No correct solution

OTHER TIPS

Your JSON reponse return the JSONArray. So you have to parse the JSONParser to JSONArray.

JSONArray json = (JSONArray )jsonparser.getJSONFromUrl(url);

If you want your response to JSONObject, you have to return like this...

{"jsonobjectname":{"nombre": "Christian", "ap_paterno": "Consuelo", "ap_materno": "Mayen", "num_telefonico": "5555555", "calle": "calle", "colonia": "colonia", "numero": 43}}

try to use [] in starting and ending

[{"nombre": "Christian", "ap_paterno": "Consuelo", "ap_materno": "Mayen",
 "num_telefonico": "5555555", "calle": "calle", "colonia": "colonia", 
 "numero": 43}]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top