java - 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;
}
解决方案
推荐阅读
- wpf - 在 MVVM 的树节点中设置条件文本
- c# - 无论如何在 ef 核心中使用表后缀名称吗?
- javascript - Recharts:关闭条形图的背景悬停
- c# - 将存储在类列表中的信息打印到控制台
- x86-64 - 如何将 rax 寄存器写入 AVX512 寄存器 zmm26 的四字元素?
- ajax - Ajax/Spring MVC handleHttpRequestMethodNotSupported 不支持请求方法“POST”
- mysql - 如何将我的“视图”存储为 mySQL 中的永久表?
- java - InteliiJ Idea SpringBoot Web 应用程序问题 - 找不到已配置/正在运行的 Web 服务器
- excel - Excel 公式中的动态引用
- sql-server - 如何引用 Oracle 数据库和 SQL Server 的 Azure 实例:它们可以链接吗?