我有一个函数应该返回一个列表,其中包含我在php制成的Web服务中拥有的所有对象,为此,我正在使用php,但这是我第一次使用webservices,我有点迷失了,事实是我正在与Volley进行咨询,我看到了一些例子,阅读了文档,但是这段代码仍然没有返回任何内容,我认为它可能是带有最终前缀的列表,但是它返回了空列表,这是函数的代码:
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;
}