首页 > 解决方案 > My consultation with volley in android does not return anything

问题描述

I have a function that should return a list with all the objects that I have in a webservice made in php, for this I am using php, but it is the first time I use webservices, and I am a bit lost, the fact is that I am doing the consultation with Volley, I saw some examples, I read the documentation, but this code still does not return anything, I thought it could be the list with the final prefix, but it returns the empty list, this is the code of the function:

public  List<Artist> getArtist() {

    final List<Artist> artistList= new ArrayList<>();
    String ip= "http://192.168.58.2:8089/ArtistBCV/consultArtists.php";
    Log.v("url",ip);

    StringRequest stringRequest = new StringRequest(Request.Method.GET, ip,
        new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                //hiding the progressbar after completion
                try {
                    Artist artist=null;
                    //getting the whole json object from the response
                    JSONObject obj = new JSONObject(response);

                    //we have the array named hero inside the object
                    //so here we are getting that json array
                    JSONArray heroArray = obj.getJSONArray("artistas");

                    //now looping through all the elements of the json array
                    for (int i = 0; i < heroArray.length(); i++) {

                        artist= new Artist();
                        JSONObject jsonObject=heroArray.getJSONObject(i);
                        Log.v("TESTO",""+jsonObject.optInt("id_artist"));
                        artist.setId_artist(jsonObject.optInt("id_artist"));
                        artist.setName_artist(jsonObject.optString("name_artist"));
                        artist.setDeathDate_artist(jsonObject.optString("deathDate_artist"));
                        artist.setBirthDate_artist(jsonObject.optString("birthDate_artist"));
                        artist.setBiography_artist(jsonObject.optString("biography_artist"));
                        artist.setPortrait_artist(jsonObject.optString("portrait_artist"));
                        artist.setPortraitIndividual(jsonObject.optString("portrait_individual"));
                        artistList.add(artist);
                    }
                } catch (JSONException e) {
                    Log.v("ERIRRIRIRIRIRI",""+e.toString());
                }
            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //displaying the error in toast if occurrs
                Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }
    );

    RequestQueue requestQueue = Volley.newRequestQueue(this);

    //adding the string request to request queue
    requestQueue.add(stringRequest);

    Log.v("!!!!!!!SIZE!!!!!!!!!", ""+artistList.size());

    return artistList;
}

标签: javaandroidrestweb-servicesandroid-volley

解决方案


推荐阅读