I have a problem, I have to get the data from a JSON file, I store the idVideo with a setter of the Video class and then in another method I can access the data with a getter, which is how it would be done normally, but the value is always null
I attach the code of the json data extraction:
public void sacarJsonInfoVideo(String url){
RequestQueue request = Volley.newRequestQueue(this);
JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject jsonObject = new JSONObject(response.toString(0));
JSONArray jsonArray=jsonObject.getJSONArray("items");
for(int i=0; i<jsonArray.length(); i++){
String idVideo=jsonArray.getJSONObject(i).getJSONObject("id").getString("videoId");
video.setIdVideo(idVideo);
String tituloVideo=jsonArray.getJSONObject(i).getJSONObject("snippet").getString("title");
video.setTitulo(tituloVideo);
String fechaSubida=jsonArray.getJSONObject(i).getJSONObject("snippet").getString("publishedAt");
String SfechaSubida=fechaSubida.substring(0, 10);
video.setDiaSubida(SfechaSubida);
String nombreCanal=jsonArray.getJSONObject(i).getJSONObject("snippet").getString("channelTitle");
video.setNombreCanal(nombreCanal);
String miniatura=jsonArray.getJSONObject(i).getJSONObject("snippet").getJSONObject("thumbnails").getJSONObject("default").getString("url");
video.setMiniatura(miniatura);
listVideo.add(video);
}
String idVideos =listVideo.get(0).getIdVideo();
System.out.println("Id a reproducir(sacarJsonInfoVideo): "+ idVideos);
tvNombreVideo.setText(listVideo.get(0).getTitulo());
tvNombreCanal.setText(listVideo.get(0).getNombreCanal());
tvFechaSubida.setText(listVideo.get(0).getDiaSubida());
} catch (JSONException e) {
siguienteVideo(v);
Toast.makeText(getApplicationContext(), "Holi", Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
request.add(jsonObjectRequest);
}
Method to access with a getter, getRandomIdVideo() is a method to get 5 random characters to do a search. The getter I store it in the sdw variable, it returns null :(
public void siguienteVideo(View v){
String idVideo=obtenerRandomIdVideo();
System.out.println("Id URL(siguienteVideo): "+idVideo);
String urlBuscarVideo="https://www.googleapis.com/youtube/v3/search?part=id,snippet&maxResults=5&type=video&q="+idVideo+"&key="+claveYT;
sacarJsonInfoVideo(urlBuscarVideo);
String sdw=video.getIdVideo();
System.out.println("Video que se va a reproducir YA: "+sdw);
}
This only happens when I try to retrieve variables from a json, is it normal for json methods?
EDIT:
I've checked several times and it's as if the video.getIdVideo() is executed before the getJsonInfoVideo(String url) method that assigns a value to video.getIdVideo().
That's what comes out of the debug console.
I/System.out: String a buscar(obtenerRandomIdVideo): 5mey1
Id URL(siguienteVideo): 5mey1
I/System.out: Video que se va a reproducir YA: null
I/System.out: Id a reproducir(sacarJsonInfoVideo): 3FTkUWUc7es
Within the getJsonInfoVideo method you are calling a JsonObjectRequest which is an asynchronous request, so when you call getJsonInfoVideo you will not get the response immediately but when it reaches the onResponse method. So this code should go inside the onResponse:
It is from this method that you call the method that interests you, passing it the value obtained