Code to get xml or Json Java [duplicate]

1

I'm trying to get the data that comes from these link
link
link

I have tried many methods but in the end I always get an error when going through this line JSONObject jsonObj = new JSONObject(json);

 OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url("http://15.26.54.26/swapi/api/data/xml")
                    .build();


            Response responses = null;

            try {
                responses = client.newCall(request).execute();

                json = responses.body().string();

                System.out.println("JSON: " + json);

                try {


                    JSONObject jsonObj = new JSONObject(json);


                    //   JSONObject obj = new JSONObject(jsonObj);

                    // Getting JSON Array node
                    JSONArray contacts = jsonObj.getJSONArray("Carro");
                    for (int i = 0; i < contacts.length(); i++) {
                        JSONObject c = contacts.getJSONObject(i);
                        String id = c.getString("idintelisis");
                        System.out.println("TESTEEEEEEEEEEEEEEEEEEEEEEE: " + id);


                    }
                } catch (JSONException ex) {

                    System.out.println("Errrrrrrrrrrrrrrrrrrrrrrrrorrrrrrrrrrrrrrrrrrr: " + ex);
                }


            } catch (IOException e) {
                e.printStackTrace();
            }

This is the long error that launches when that line bursts

  

01-11 10: 04: 47.487 6541-7024 / pa.com.tropigas.pedidostf W / System.err: org.json.JSONException: Value [{"idintelisis": 101, "observations": "Nitin" , "date": null, "car": null, "cargapkt": "sd5"}, {"idintelisis": 101, "observations": "Nitin", "date": null, "car": null, " loadpkt ":" sd5 "}] of type java.lang.String can not be converted to JSONObject   01-11 10: 04: 47.487 6541-7024 / pa.com.tropigas.pedidostf W / System.err: at org.json.JSON.typeMismatch (JSON.java)   01-11 10: 04: 47.487 6541-7024 / pa.com.tropigas.pedidostf W / System.err: at org.json.JSONObject. (JSONObject.java)   01-11 10: 04: 47.487 6541-7024 / pa.com.tropigas.pedidostf W / System.err: at org.json.JSONObject. (JSONObject.java)   01-11 10: 04: 47.487 6541-7024 / pa.com.tropigas.pedidostf W / System.err: at pa.com.tropigas.pedidostf.CarrosActivity $ POST.doInBackground (CarrosActivity.java:114)   01-11 10: 04: 47.487 6541-7024 / pa.com.tropigas.pedidostf W / System.err: at pa.com.tropigas.pedidostf.CarrosActivity $ POST.doInBackground (CarrosActivity.java:65)   01-11 10: 04: 47.487 6541-7024 / pa.com.tropigas.pedidostf W / System.err: at android.os.AsyncTask $ 2.call (AsyncTask.java)   01-11 10: 04: 47.487 6541-7024 / pa.com.tropigas.pedidostf W / System.err: at java.util.concurrent.FutureTask.run (FutureTask.java)   01-11 10: 04: 47.487 6541-7024 / pa.com.tropigas.pedidostf W / System.err: at android.os.AsyncTask $ SerialExecutor $ 1.run (AsyncTask.java)   01-11 10: 04: 47.487 6541-7024 / pa.com.tropigas.pedidostf W / System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java)   01-11 10: 04: 47.487 6541-7024 / pa.com.tropigas.pedidostf W / System.err: at java.util.concurrent.ThreadPoolExecutor $ Worker.run (ThreadPoolExecutor.java)   01-11 10: 04: 47.487 6541-7024 / pa.com.tropigas.pedidostf W / System.err: at java.lang.Thread.run (Thread.java)   01-11 10: 04: 47.487 6541-7024 / pa.com.tropigas.pedidostf I / System.out: Error: org.json.JSONException: Value [{"idintelisis": 101, "observations": "Nitin", "date": null, "car": null, "cargapkt": "sd5"}, {"idintelisis": 101, "observations": "Nitin", "date": null, "car": null, "cargapkt ":" sd5 "}] of type java.lang.String can not be converted to JSONObject

I've used Postman to review the result of Json and also the Json of the log and everything indicates that the Json is correct in terms of structure I do not understand why it breaks when trying to convert it to an object of Json

    
asked by Heisenberg06 11.01.2017 в 16:11
source

2 answers

1

This will be the solution to the problem

The object I'm trying to capture is JsonArray no JsonObject

try this

JsonArray arr = new JsonArray (resultado obtenido) ;
And then try to get JsonObject of arr using the method getJSONObject(arr)

Example:

JsonArray arr = new JsonArray (resultado obtenido);
    Para (int i = 0; i <arr.length (); i ++) {
    JsonObject ob = arr.getJSONObject (i);
}
    
answered by 11.01.2017 в 19:51
0

What happens is that you are sending an array of objects in the api, when you must send a single object, you just have to put the original array in "{}" that is, your api should return the following:

{ arreglo : [{"idintelisis":101,"obser..." ... (resto de contenido del JSON) ] } 
    
answered by 11.01.2017 в 18:02