Items are missing when receiving Array

5

Everything works correctly for me by only collecting the values of an array, but when I collect the data of the two that is what I want, I am missing data, as I can do it to have them in the same activity:

@Override
    protected String doInBackground(String... url) {

        urlPost = url[0];
        try {
            jsonObjectDesignPosts = JsonParser.readJsonFromUrl(urlPost);
            postNumber = jsonObjectDesignPosts.getJSONArray("golesLocal").length();
            jsonArrayDesignContent = jsonObjectDesignPosts.getJSONArray("golesLocal");

            postNumber = jsonObjectDesignPosts.getJSONArray("golesVisitante").length();
            jsonArrayDesignContent = jsonObjectDesignPosts.getJSONArray("golesVisitante");

            sharedPreferences.edit().putString("DESIGN", jsonArrayDesignContent.toString()).apply();
            designNombre_local = new String[postNumber];
            designMinuto_local = new String[postNumber];

            designNombre_visitante = new String[postNumber];
            designMinuto_visitante = new String[postNumber];


            for (int i = 0; i < postNumber; i++) {

                designNombre_local[i] = Html.fromHtml(jsonObjectDesignPosts.getJSONArray("golesLocal").getJSONObject(i).getString("nombre")).toString();
                designMinuto_local[i] = Html.fromHtml(jsonObjectDesignPosts.getJSONArray("golesLocal").getJSONObject(i).getString("minuto")).toString();
                designNombre_visitante[i] = Html.fromHtml(jsonObjectDesignPosts.getJSONArray("golesVisitante").getJSONObject(i).getString("nombre")).toString();
                designMinuto_visitante[i] = Html.fromHtml(jsonObjectDesignPosts.getJSONArray("golesVisitante").getJSONObject(i).getString("minuto")).toString();

            }

        } catch (IOException | JSONException e) {
            e.printStackTrace();
            designNombre_local = new String[0];
            error = true;
        }
        return null;
    }

@Override
protected void onPostExecute(String result) {

    designs = new ArrayList<>();


    if (designNombre_local.length != -1) {


        for(int i=0; i<(designNombre_local.length); i++){

            designs.add(new Goles(designNombre_local[i],designMinuto_local[i],
                    designNombre_visitante[i],designMinuto_visitante[i]));
        }
    }
    if (error) {
        Toast.makeText(getActivity(), "Error de conexión", Toast.LENGTH_LONG).show();
    }

I noticed that it shows the number of items below, I mean, in the game they marked the premises 5 and the visitors 3, because it shows only three results of each team. What is the reason?

    
asked by Rafel C.F 03.01.2016 в 15:11
source

3 answers

1

When sizing how many values you will get from your array you are considering that postNumber has the same value for local and visitor, even if golesLocal were more or less at the end you are taking the value of golesVisitante :

postNumber = jsonObjectDesignPosts.getJSONArray("golesLocal").length();
postNumber = jsonObjectDesignPosts.getJSONArray("golesVisitante").length();

The problem happens here:

    for (int i = 0; i < postNumber; i++) {

        designNombre_local[i] = Html.fromHtml(jsonObjectDesignPosts.getJSONArray("golesLocal").getJSONObject(i).getString("nombre")).toString();
        designMinuto_local[i] = Html.fromHtml(jsonObjectDesignPosts.getJSONArray("golesLocal").getJSONObject(i).getString("minuto")).toString();
        designNombre_visitante[i] = Html.fromHtml(jsonObjectDesignPosts.getJSONArray("golesVisitante").getJSONObject(i).getString("nombre")).toString();
        designMinuto_visitante[i] = Html.fromHtml(jsonObjectDesignPosts.getJSONArray("golesVisitante").getJSONObject(i).getString("minuto")).toString();

    }

You might even have the error:

  

ArrayIndexOutOfBoundsException

As a solution, you should take a value postNumber of golesLocal and another of postNumber for golesVisitante , this to size the correct obtaining of values, example:

//Obtiene valores Local.
    postNumber = jsonObjectDesignPosts.getJSONArray("golesLocal").length();


        for (int i = 0; i < postNumber; i++) {

            designNombre_local[i] = Html.fromHtml(jsonObjectDesignPosts.getJSONArray("golesLocal").getJSONObject(i).getString("nombre")).toString();
            designMinuto_local[i] = Html.fromHtml(jsonObjectDesignPosts.getJSONArray("golesLocal").getJSONObject(i).getString("minuto")).toString();

        }

//Obtiene valores Visitante.
    postNumber = jsonObjectDesignPosts.getJSONArray("golesVisitante").length();

        for (int i = 0; i < postNumber; i++) {

            designNombre_visitante[i] = Html.fromHtml(jsonObjectDesignPosts.getJSONArray("golesVisitante").getJSONObject(i).getString("nombre")).toString();
            designMinuto_visitante[i] = Html.fromHtml(jsonObjectDesignPosts.getJSONArray("golesVisitante").getJSONObject(i).getString("minuto")).toString();

        }
    
answered by 02.12.2016 / 17:00
source
1

The problem I think is here postNumber in this variable assignment / reassignment.

pseudo code:

//..
//se marcan 5 goles
postNumber = jsonObjectDesignPosts.getJSONArray("golesLocal").length();

//ahora se marcan 3   
postNumber = jsonObjectDesignPosts.getJSONArray("golesVisitante").length();

and now here how many are displayed - >

for (int i = 0; i < postNumber; i++) {

     designNombre_local[i]     = Html.fromHtml(jsonObjectDesignPosts.getJSONArray("golesLocal").getJSONObject(i).getString("nombre")).toString();
     designMinuto_local[i]     = Html.fromHtml(jsonObjectDesignPosts.getJSONArray("golesLocal").getJSONObject(i).getString("minuto")).toString();
     designNombre_visitante[i] = Html.fromHtml(jsonObjectDesignPosts.getJSONArray("golesVisitante").getJSONObject(i).getString("nombre")).toString();
     designMinuto_visitante[i] = Html.fromHtml(jsonObjectDesignPosts.getJSONArray("golesVisitante").getJSONObject(i).getString("minuto")).toString();

}

The last value assigned to it is 3 for example (it will only iterate 3 times, that's why in this case you lack goals from a team)

I hope you understand me, you can solve it in many ways if the error is the one I indicated (I could not test it), you could create two variables postNumberLocal and postNumberVisitiante and iterate them separately or I mean a for for example for each varariable but it is only an idea.

for (int i = 0; i < postNumberLocal; i++) {

     designNombre_local[i] = Html.fromHtml(jsonObjectDesignPosts.getJSONArray("golesLocal")
                               .getJSONObject(i)
                               .getString("nombre")).toString();

     designMinuto_local[i] = Html.fromHtml(jsonObjectDesignPosts.getJSONArray("golesLocal")
                               .getJSONObject(i)
                               .getString("minuto")).toString();

}

for (int i = 0; i < postNumberVisitiante; i++) {


     designNombre_visitante[i] = Html.fromHtml(jsonObjectDesignPosts.getJSONArray("golesVisitante")
                               .getJSONObject(i)
                               .getString("nombre")).toString();

     designMinuto_visitante[i] = Html.fromHtml(jsonObjectDesignPosts.getJSONArray("golesVisitante")
                               .getJSONObject(i)
                               .getString("minuto")).toString();

}

Now we adjust the new variables

//.. 
//se marcan 5 goles
postNumberLocal      = jsonObjectDesignPosts.getJSONArray("golesLocal").length();
//..
//ahora se marcan 3   
postNumberVisitiante = jsonObjectDesignPosts.getJSONArray("golesVisitante").length();
//..

And also at the time of determining the size of the arrays that you use something like this:

//..

..//
designNombre_local     = new String[postNumberLocal];
designMinuto_local     = new String[postNumberLocal];
designNombre_visitante = new String[postNumberVisitante];
designMinuto_visitante = new String[postNumberVisitante];
..//
    
answered by 04.01.2016 в 03:54
1

You tell me - >

  

The application, logcat:   java.lang.ArrayIndexOutOfBoundsException: length = 3; index = 3

that happens basically because you are trying to access outside the limits of the array (in this case) simple illustration:

  • The size of your array, let's say it's 3.
  • And you are trying to access the position (index) 3.

Aparentemete may think that it is fine, but in many languages the indexed is from 0, that means that counts from 0.

 para la longitud        el indice maximo es 
    1                          0
    2                          1
    3                          2

.

Taking into account the above you are using at some point for the longitud = 3 an access to indice 3 of the array, as you can see in the previous table the maximum for 3 is 2, hence your error.

Now how to solve it I do not know where exactly the error occurs at first glance based on the log it shows, because I can not see that line, but I think that understanding the above you can find out where the error is, (in the output of the log, it is very likely that you will indicate a number, say 10, because you go to line ten of your code and look for the error there, this would be a simple way to find the error).

But if I have to say where I think the error is, it may be in this part of the code:

  if (designNombre_local.length != -1) {


    for(int i=0; i<(designNombre_local.length); i++){

        designs.add(new Goles(designNombre_local[i],designMinuto_local[i],
                designNombre_visitante[i],designMinuto_visitante[i]));
    }

More specifically, this line is:

  

designNombre_visitante [i], designMinuto_visitante [i]));

It is possible that the size of designNombre_visitante is different (smaller) than designNombre_local.length of there that at some point the variable i try to access outside the index of the array designNombre_visitante

how to solve it exactly if that is the error, taking into account what is the reason for the error, you should know what you are intending to do with:

designs.add(new Goles(designNombre_local[i],designMinuto_local[i],
                    designNombre_visitante[i],designMinuto_visitante[i]));

And in what way it is used (trying or adjusting it in some way so that you do not try to access a non-valid array position).

P.D: as a note I will also tell you that you may be assuming that design_local_name.length, always greater than design_visiting_name,

for(int i=0; i<(designNombre_local.length); i++){

but I do not know if that is your intent, but if you do not take it into account when you make the changes to solve the previous error.

    
answered by 06.01.2016 в 10:44