Veréis, tengo este .Json
el cual tiene varios arrays lo que quisiera es ver en dos listas, goles y minuto local en una y goles y minuto visitante en otra, pero me falla algo. Me pueden ayudar. Gracias
public class Goles_Adapter extends ArrayAdapter {
// Atributos
private RequestQueue requestQueue;
JsonObjectRequest jsArrayRequest;
private static final String URL_BASE = "http://www.ffcv.es/ncompeticiones/server.php?action=getActa&tmp=2015/2016&jor=9&cmp=284&idl=0201045301&idv=0201239301";
private static final String URL_JSON = "";
private static final String TAG = "";
private static final String ESCUDO = "http://ffcv.es/ncompeticiones/";
List<Goles> items;
List<Goles_V> items_v;
public Goles_Adapter(Context context) {
super(context,0);
// Crear nueva cola de peticiones
requestQueue= Volley.newRequestQueue(context);
// Nueva peticion JSONObject
jsArrayRequest = new JsonObjectRequest(
Request.Method.GET,
URL_BASE + URL_JSON,
null,
new Response.Listener<JSONObject>() {
@override
public void onResponse(JSONObject response) {
items = parseJson(response);
notifyDataSetChanged();
}
},
new Response.ErrorListener() {
@override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "Error Respuesta en JSON: " + error.getMessage());
}
}
);
// Anyadir peticion a la cola
requestQueue.add(jsArrayRequest);
}
@override
public int getCount() {
return items != null ? items.size() : 0;
}
@override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
// Referencia del view procesado
View listItemView;
//Comprobando si el View no existe
listItemView = null == convertView ? layoutInflater.inflate(
R.layout.goles_row,
parent,
false) : convertView;
// Obtener el item actual
Goles item = items.get(position);
// Obtener Views
TextView textoNombre = (TextView) listItemView.findViewById(R.id.tv_jornada);
TextView textoMinuto = (TextView) listItemView.findViewById(R.id.tv_Fecha);
// Actualizar los Views
textoNombre.setText(item.getNombre());
textoMinuto.setText(item.getMinuto());
View listItemView1;
listItemView1 = null == convertView ? layoutInflater.inflate(
R.layout.goles_row,
parent,
false) : convertView;
Goles_V item1 = items_v.get(position);
// Obtener Views
TextView textoNombre1 = (TextView) listItemView1.findViewById(R.id.tv_jornada1);
TextView textoMinuto1 = (TextView) listItemView1.findViewById(R.id.tv_Fecha1);
// Actualizar los Views
textoNombre1.setText(item1.getNombre());
textoMinuto1.setText(item1.getMinuto());
// Anyadir peticion a la cola
return listItemView;
}
public List<Goles> parseJson(JSONObject jsonObject){
// Variables locales
List<Goles> rankingAmonestacionesCadetes = new ArrayList<>();
JSONArray jsonArray= null;
try {
// Obtener el array del objeto
jsonArray = jsonObject.getJSONArray("golesLocal");
for(int i=0; i<jsonArray.length(); i++){
try {
JSONObject objeto= jsonArray.getJSONObject(i);
Goles RankingAmonestacionesCadetes = new Goles(
objeto.getString("nombre"),
objeto.getString("minuto"));
rankingAmonestacionesCadetes.add(RankingAmonestacionesCadetes);
} catch (JSONException e) {
Log.e(TAG, "Error de parsing: " + e.getMessage());
}
}
} catch (JSONException e) {
e.printStackTrace();
}
public List<Goles_V> parseJson(JSONObject jsonObject){
// Variables locales
List<Goles_V> goles_Visitante = new ArrayList<>();
JSONArray jsonArray_visi= null;
try {
// Obtener el array del objeto
jsonArray_visi = jsonObject.getJSONArray("golesVisitante");
for(int i=0; i<jsonArray_visi.length(); i++){
try {
JSONObject objeto= jsonArray_visi.getJSONObject(i);
Goles_V goles_visitante = new Goles_V(
objeto.getString("nombre"),
objeto.getString("minuto"));
goles_Visitante.add(goles_visitante);
} catch (JSONException e) {
Log.e(TAG, "Error de parsing: " + e.getMessage());
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return rankingAmonestacionesCadetes;
}
}
}
Pero en la linea
public List<Goles_V> parseJson(JSONObject jsonObject){
hay algo mal.
Error155, 9) error: illegal start of expression Error155, 39) error: ';' expected Error155, 61) error: ';' expected Error:Execution failed for task ':app:compileDebugJavaWithJavac'. > Compilation failed; see the compiler error output for details.
Bien e conseguido a medias este codigo me funciona correctamente cuando hay el mismo numero de items en cada array, osea empate, pero cuando hay mas items de locales que visitantes da este error:
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
este es el codigo:
public class Goles_Adapter_J25 extends ArrayAdapter {
// Atributos
private RequestQueue requestQueue;
JsonObjectRequest jsArrayRequest;
private static final String URL_BASE = "http://juveniles.esy.es/2015/jornadas/info_jornadas/info_j_25.php";
private static final String URL_JSON = "";
private static final String TAG = "";
private static final String ESCUDO = "http://ffcv.es/ncompeticiones/";
List<Goles_Local> items_local;
List<Goles_Visi> items_visi;
public Goles_Adapter_J25(Context context) {
super(context,0);
// Crear nueva cola de peticiones
requestQueue= Volley.newRequestQueue(context);
// Nueva peticion JSONObject
jsArrayRequest = new JsonObjectRequest(
Request.Method.GET,
URL_BASE + URL_JSON,
null,
new Response.Listener<JSONObject>() {
public void onResponse(JSONObject response) {
items_local = parseJson_local(response);
items_visi = parseJson_visitante(response);
notifyDataSetChanged();
}
},
new Response.ErrorListener() {
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "Error Respuesta en JSON: " + error.getMessage());
}
}
);
// Anyadir peticion a la cola
requestQueue.add(jsArrayRequest);
}
public int getCount() {
return items_local != null ? items_local.size() : 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
// Referencia del view procesado
View listItemView;
//Comprobando si el View no existe
listItemView = null == convertView ? layoutInflater.inflate(
R.layout.goles_dos_listas_row,
parent,
false) : convertView;
// Obtener el item actual
Goles_Local item = items_local.get(position);
// Obtener Views
TextView textoNombre = (TextView) listItemView.findViewById(R.id.tv_Nombre_Local);
TextView textoMinuto = (TextView) listItemView.findViewById(R.id.tv_Minuto_Local);
// Actualizar los Views
textoNombre.setText(item.getNombre());
textoMinuto.setText(item.getMinuto());
// Obtener el item actual
Goles_Visi item_visi = items_visi.get(position);
// Obtener Views
TextView textoNombre1 = (TextView) listItemView.findViewById(R.id.tv_Nombre_Visi);
TextView textoMinuto1 = (TextView) listItemView.findViewById(R.id.tv_Minuto_Visi);
// Actualizar los Views
textoNombre1.setText(item_visi.getNombre_visi());
textoMinuto1.setText(item_visi.getMinuto_visi());
// Anyadir peticion a la cola
return listItemView;
}
public List<Goles_Local> parseJson_local(JSONObject jsonObject) {
// Variables locales
List<Goles_Local> goles_Local_local = new ArrayList<>();
JSONArray jsonArray_Local = null;
try {
// Obtener el array del objeto
jsonArray_Local = jsonObject.getJSONArray("golesLocal");
for (int i = 0; i < jsonArray_Local.length(); i++) {
try {
JSONObject objeto = jsonArray_Local.getJSONObject(i);
Goles_Local goles_Local_Local = new Goles_Local(
objeto.getString("nombre"),
objeto.getString("minuto"));
goles_Local_local.add(goles_Local_Local);
} catch (JSONException e) {
Log.e(TAG, "Error de parsing: " + e.getMessage());
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return goles_Local_local;
}
public List<Goles_Visi> parseJson_visitante (JSONObject jsonObject){
// Variables locales
List<Goles_Visi> goles_Visitante = new ArrayList<>();
JSONArray jsonArray_visi = null;
try {
// Obtener el array del objeto
jsonArray_visi = jsonObject.getJSONArray("golesVisitante");
for (int i = 0; i < jsonArray_visi.length(); i++) {
try {
JSONObject objeto = jsonArray_visi.getJSONObject(i);
Goles_Visi goles_visitante = new Goles_Visi(
objeto.getString("minuto"),
objeto.getString("nombre"));
goles_Visitante.add(goles_visitante);
} catch (JSONException e) {
Log.e(TAG, "Error de parsing: " + e.getMessage());
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return goles_Visitante;
}
}
json
interesante, ¿de qué manera haz definido tuPOJO
para realizar la conversiónjson/java
?