0

I need help on creating an app that parse the JSON data file. I got an error when I run the code.

Error code

05-28 17:10:31.246 7317-7325/? E/DataBuffer: Internal data leak within a DataBuffer object detected! Be sure to explicitly call release() on all DataBuffer extending objects when you are done with them.
(internal object: com.google.android.gms.common.data.DataHolder@2eed538) 05-28 17:10:32.108 7867-8139/? E/GmsUtils: Failed to connect to Google API
client: ConnectionResult{statusCode=API_UNAVAILABLE, resolution=null, message=null} 05-28 17:13:04.755 5599-5599/? E/Ads: JS: Uncaught ReferenceError: AFMA_ReceiveMessage is not defined (:1)

MainActivity.java

public class MainActivity extends Activity {

/**
 * ATTENTION: This was auto-generated to implement the App Indexing API.
 * See https://g.co/AppIndexing/AndroidStudio for more information.
 */
private GoogleApiClient client;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView output = (TextView) findViewById(R.id.textView1);
    String strJson = "" +
            "{" +
            "\"Carrot\":[" +

            "[{\"name\" : \"Arkansas #19\", \"description\" : \"Modelo: March\n" +
            "Entre calles: Montana y Nebrazka, Col. Nápoles\n" +
            "Horas de Ocupación: 179.1\n" +
            "Latitud: 19.392175\n" +
            "Longitud: -99.173659\", \"coordinates\" : \"-99.173659,19.392175,0.0\"},{\"name\" : \"Av. Colonia del Valle #101\", \"description\" : \"Modelo: Xtrail\n" +
            "Entre calles: Sacramento e Insurgentes Sur, Col. Del Valle\n" +
            "Horas de Ocupación: 133.2\n" +
            "Latitud: 19.38603\n" +
            "Longitud: -99.174609\", \"coordinates\" : \"-99.174609,19.38603,0.0\"},{\"name\" : \"Bajo Puente Juan Escutia\", \"description\" : \"Modelo: Xtrail / Leaf\n" +
            "Entre calles: Juan Escutia y Juan de la Barrera\n" +
            "Horas de Ocupación: 80.0 / 74.8\n" +
            "Latitud: 19.417246\n" +
            "Longitud: -99.179057\", \"coordinates\" : \"-99.179057,19.417246,0.0\"},{\"name\" : \"Bajo Puente Universidad\", \"description\" : \"Modelo: March / Leaf\n" +
            "Entre calles: Cto Interior y Av. Universidad\n" +
            "Horas de Ocupación: 68.6 / 60.3\n" +
            "Latitud: 19.360371\n" +
            "Longitud: -99.173166\", \"coordinates\" : \"-99.173166,19.360371,0.0\"},{\"name\" : \"Chilpancingo #52\", \"description\" : \"Modelo: March\n" +
            "Entre calles: Tlaxcala y Aguascalientes, Col. Condesa.\n" +
            "Horas de Ocupación: 60.2\n" +
            "Latitud: 19.407537\n" +
            "Longitud: -99.169555\", \"coordinates\" : \"-99.169555,19.407537,0.0\"},{\"name\" : \"Coahuila #193\", \"description\" : \"Modelo: March\n" +
            "Entre calles: Medellín y Manzanillo, Col Roma Norte\n" +
            "Horas de Ocupación: 59.6\n" +
            "Latitud: 19.410937\n" +
            "Longitud: -99.165092\", \"coordinates\" : \"-99.165092,19.410937,0.0\"},{\"name\" : \"Dakota #95\", \"description\" : \"Modelo: March\n" +
            "Entre calles: Estacionamiento del Hotel Crowne Plaza\n" +
            "Horas de Ocupación: 59.3\n" +
            "Latitud: 19.395976\n" +
            "Longitud: -99.174309\", \"coordinates\" : \"-99.174309,19.395976,0.0\"},{\"name\" : \"Dante #13\", \"description\" : \"Modelo: Xtrail\n" +
            "Entre calles: Victor Hugo y Tolstoi, Col. Anzures\n" +
            "Horas de Ocupación: 58.0\n" +
            "Latitud: 19.426498\n" +
            "Longitud: -99.178164\", \"coordinates\" : \"-99.178164,19.426498,0.0\"},{\"name\" : \"Ejército Nacional 904\", \"description\" : \"Modelo: Xtrail / March\n" +
            "Entre calles: Sófocles y Sócrates.\n" +
            "Horas de Ocupación: 56.4 /55.4\n" +
            "Latitud: 19.438367\n" +
            "Longitud: -99.204361\", \"coordinates\" : \"-99.204361,19.438367,0.0\"},{\"name\" : \"Federico T de la Chica #18\", \"description\" : \"Modelo: March\n" +
            "Entre calles: Circuito Centro Comercial, Cd. Satélite\n" +
            "Horas de Ocupación: 53.7\n" +
            "Latitud: 19.513296\n" +
            "Longitud: -99.230909\", \"coordinates\" : \"-99.230909,19.513296,0.0\"},{\"name\" : \"Gabriel Mancera #1536\", \"description\" : \"Modelo: March\n" +
            "Entre calles: Parroquia y Felix Cuevas\n" +
            "Horas de Ocupación: 52.8\n" +
            "Latitud: 19.369917\n" +
            "Longitud: -99.168287\", \"coordinates\" : \"-99.168287,19.369917,0.0\"},{\"name\" : \"General Primm #82\", \"description\" : \"Modelo: March\n" +
            "Entre calles: Millan y Lisboa, Col. Juárez\n" +
            "Horas de Ocupación: 52.5\n" +
            "Latitud: 19.430741\n" +
            "Longitud: -99.155839\", \"coordinates\" : \"-99.155839,19.430741,0.0\"},{\"name\" : \"Gobernador Agustin Vicente Eguía #9\", \"description\" : \"Modelo: March\n" +
            "Entre calles: Av. Revolucion y Gral Juan Cano.\n" +
            "Horas de Ocupación: 49.3\n" +
            "Latitud: 19.407757\n" +
            "Longitud: -99.184936\", \"coordinates\" : \"-99.184936,19.407757,0.0\"},{\"name\" : \"González Camarena 999\", \"description\" : \"Modelo: March\n" +
            "Entre calles: Estacionamineto de Plaza Pasaje Santa Fe\n" +
            "Horas de Ocupación: 49.0\n" +
            "Latitud: 19.367000\n" +
            "Longitud: -99.262000\", \"coordinates\" : \"-99.262,19.367,0.0\"},{\"name\" : \"Horacio # 312\", \"description\" : \"Modelo: March / Prius\n" +
            "Entre calles: Entre Schiller y Petrarca\n" +
            "Horas de Ocupación: 47.6 /47.5\n" +
            "Latitud: 19.433119\n" +
            "Longitud: -99.186188\", \"coordinates\" : \"-99.186188,19.433119,0.0\"},{\"name\" : \"Insurgentes Norte #130\", \"description\" : \"Modelo: March\n" +
            "Entre calles: Col. Sta María La Ribera entre Sor Juana Inés de la Cruz y Amado Nervo.\n" +
            "Horas de Ocupación: 47.2\n" +
            "Latitud: 19.444258\n" +
            "Longitud: -99.154163\", \"coordinates\" : \"-99.154163,19.444258,0.0\"},{\"name\" : \"Liverpool #179\", \"description\" : \"Modelo: March / Leaf\n" +
            "Entre calles: Av. Chapultepec y florencia\n" +
            "Horas de Ocupación: 46.7 / 44.3\n" +
            "Latitud: 19.423373\n" +
            "Longitud: -99.166305\", \"coordinates\" : \"-99.166305,19.423373,0.0\"},{\"name\" : \"Londres #91\", \"description\" : \"Modelo: March / Prius\n" +
            "Entre calles: Amberes y Niza, Col Juárez.\n" +
            "Horas de Ocupación: 43.3 / 42.4 \n" +
            "Latitud: 19.426109\n" +
            "Longitud: -99.162753\", \"coordinates\" : \"-99.162753,19.426109,0.0\"},{\"name\" : \"Manuel M Contreras 61\", \"description\" : \"Modelo: March\n" +
            "Entre calles: Francisco Pimentel y Guillermo Prieto\n" +
            "Horas de Ocupación: 40.5\n" +
            "Latitud: 19.438666\n" +
            "Longitud: -99.16494\", \"coordinates\" : \"-99.16494,19.438666,0.0\"},{\"name\" : \"Matanzas #668\", \"description\" : \"Modelo: March\n" +
            "Entre calles: Montevideo y Buenavista, Col. Lindavista\n" +
            "Horas de Ocupación: 40.2\n" +
            "Latitud: 19.490964\n" +
            "Longitud: -99.132257\", \"coordinates\" : \"-99.132257,19.490964,0.0\"},{\"name\" : \"Mérida #68 Col. Roma\", \"description\" : \"Modelo: Xtrail\n" +
            "Entre calles: Colima y Durango, Col. Roma\n" +
            "Horas de Ocupación: 39.5\n" +
            "Latitud: 19.421182\n" +
            "Longitud: -99.157781\", \"coordinates\" : \"-99.157781,19.421182,0.0\"},{\"name\" : \"Mérida #71\", \"description\" : \"Modelo: March / March\n" +
            "Entre calles: Colima y Durango, Col. Roma\n" +
            "Horas de Ocupación: 39.0 / 38.8\n" +
            "Latitud: 19.421147\n" +
            "Longitud: -99.158028\", \"coordinates\" : \"-99.158028,19.421147,0.0\"},{\"name\" : \"Moctezuma #115, Del Carmen\", \"description\" : \"Modelo: March\n" +
            "Entre calles: Caballo Calco y Abasolo, Col. Del Carmen Coyoacan\n" +
            "Horas de Ocupación: 38.5\n" +
            "Latitud: 19.350647\n" +
            "Longitud: -99.16084\", \"coordinates\" : \"-99.16084,19.350647,0.0\"},{\"name\" : \"Moliere #307\", \"description\" : \"Modelo: Xtrail / Prius / March\n" +
            "Entre calles: Homero y Ejercito Nacional, Col. Polanco\n" +
            "Horas de Ocupación: 36.8 / 33.4 / 33.4\n" +
            "Latitud: 19.436769\n" +
            "Longitud: -99.201114\", \"coordinates\" : \"-99.201114,19.436769,0.0\"},{\"name\" : \"Parque Carrot\", \"description\" : \"Modelo: Xtrail / Prius / March\n" +
            "Entre calles: Michoacán esq. Vicente Suárez\n" +
            "Horas de Ocupación: 32.9 / 31.0 / 28.0 \n" +
            "Latitud: 19.411547\n" +
            "Longitud: -99.175304\", \"coordinates\" : \"-99.175304,19.411547,0.0\"},{\"name\" : \"Paseo de la Reforma 157.\", \"description\" : \"Modelo: Xtrail\n" +
            "Entre calles: Esqina Insurgentes\n" +
            "Horas de Ocupación: 26.1\n" +
            "Latitud: 19.431202\n" +
            "Longitud: -99.159799\", \"coordinates\" : \"-99.159799,19.431202,0.0\"},{\"name\" : \"Plaza Las Aguilas\", \"description\" : \"Modelo: March\n" +
            "Entre calles: Condor y las Aguilas.\n" +
            "Horas de Ocupación: 23.9\n" +
            "Latitud: 19.361144\n" +
            "Longitud: -99.197136\", \"coordinates\" : \"-99.197136,19.361144,0.0\"},{\"name\" : \"Puente de la Morena 95\", \"description\" : \"Modelo: March\n" +
            "Entre calles: Esquina Patriotismo\n" +
            "Horas de Ocupación: 21.1\n" +
            "Latitud: 19.396137\n" +
            "Longitud: -99.181473\", \"coordinates\" : \"-99.181473,19.396137,0.0\"},{\"name\" : \"Río de la Plata #20\", \"description\" : \"Modelo: March\n" +
            "Entre calles: Río Lerma y Río Panuco\n" +
            "Horas de Ocupación: 19.3\n" +
            "Latitud: 19.42744\n" +
            "Longitud: -99.174296\", \"coordinates\" : \"-99.174296,19.42744,0.0\"},{\"name\" : \"Río Tiber #93\", \"description\" : \"Modelo: March\n" +
            "Entre calles: Río Lerma y Río Panuco\n" +
            "Horas de Ocupación: 19.0\n" +
            "Latitud: 19.429157\n" +
            "Longitud: -99.168865\", \"coordinates\" : \"-99.168865,19.429157,0.0\"},{\"name\" : \"Xola #321\", \"description\" : \"Modelo: March\n" +
            "Entre calles: Cda de Xola y Av. Coyoacan\n" +
            "Horas de Ocupación: 18.2\n" +
            "Latitud: 19.397688\n" +
            "Longitud: -99.167813\", \"coordinates\" : \"-99.167813,19.397688,0.0\"},{\"name\" : \"Zamora #11\", \"description\" : \"Modelo: March\n" +
            "Entre calles: Veracruz y Agustín Melgar\n" +
            "Horas de Ocupación: 15.9\n" +
            "Latitud: 19.419174\n" +
            "Longitud: -99.17647\", \"coordinates\" : \"-99.17647,19.419174,0.0\"}]";

    String data = "";
    try {
        JSONObject jsonRootObject = new JSONObject(strJson);
        // Get the instance of JSONArray that contains JSONObjects
        JSONArray jsonArray = jsonRootObject.optJSONArray("Carrot");

        //Iterate the jsonArray and print the info of JSONObjects
        for (int i=0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);

            int coordinates = Integer.parseInt(jsonObject.optString("coordinates").toString());
            String name = jsonObject.optString("name").toString();

            data += "Node"+i+" : \n coordinates= "+ coordinates +" \n Name = "+ name +"\n";

        }

        output.setText(data);

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

}
}

Your help is much appreciated. Thanks!

SierraOscar
  • 17,507
  • 6
  • 40
  • 68
Cassie.L
  • 311
  • 1
  • 7
  • 19
  • Those errors have nothing to do with the parsing of your JSON... – OneCricketeer May 28 '16 at 21:38
  • And seeing as your coordinates aren't integers, I don't think `Integer.parseInt(jsonObject.optString("coordinates"))` will return anything useful – OneCricketeer May 28 '16 at 21:40
  • Thanks for replying, so you think i should change it to string? – Cassie.L May 28 '16 at 21:40
  • I think you should provide the full logcat or add some `Log.i` statements to see if you are getting the data you want – OneCricketeer May 28 '16 at 21:41
  • how can I do that, or where can I add that? I am new to coding and I was following the tutorial that I find online – Cassie.L May 28 '16 at 21:42
  • `Log.i(String tag, String message)` will print info messages to the console. Similar to the function of `System.out.println()` in java, which you can also do in Android, but Log is the correct way. – OneCricketeer May 28 '16 at 21:44
  • I didn't get the statements that I want to see on the app – Cassie.L May 28 '16 at 21:44
  • Where Should I add the Log.i(String tag, String message) into? – Cassie.L May 28 '16 at 21:45
  • Anywhere you want, really... At the start of the parsing... In the middle of it... After it.... – OneCricketeer May 28 '16 at 21:46
  • What should I replace the string tag, string message regarding to my data? – Cassie.L May 28 '16 at 21:47
  • the tag can be "Cassie.L", or anything you will identify in the logs, and the message can be `"Parsing JSON..."` or `jsonObject.toString()`... This is called manual debugging, and it is a very important step of programming that can't really be taught on StackOverflow. If you want a full answer, I suggest you [edit] your post with the full exception message rather than those 3 lines. – OneCricketeer May 28 '16 at 21:49
  • So after I change the integer to string, I didnt get any error. However, the app does not show the data that I requested on mainActivity. – Cassie.L May 28 '16 at 21:55
  • Not sure what to tell you, then. Best to make a [mcve], though, because your JSON is difficult to read as-is. – OneCricketeer May 28 '16 at 21:59

0 Answers0