Well I'm starting in the android area and I still do not know much about it and would like help with the following code:
gridView = (GridView) findViewById(R.id.lastedAnimeView);
gridView.setAdapter(va);
mRequestQueue = Volley.newRequestQueue(this);
String lastedAnimesUrl = "http://10.0.0.13/anime/request/lastedAnimes.php";
JsonObjectRequest jr = new JsonObjectRequest(Request.Method.POST, lastedAnimesUrl, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.i(TAG, response.toString());
parseJSON(response);
va.notifyDataSetChanged();
pd.dismiss();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i(TAG, error.getMessage());
}
});
mRequestQueue.add(jr);
}
private void parseJSON(JSONObject json){
try {
JSONArray items = json.getJSONArray("result");
for(int i=0;i<items.length();i++){
JSONObject item = items.getJSONObject(i);
NewsModel nm = new NewsModel();
nm.setNome(item.optString("Nome"));
URL urlGetImg = new URL(item.optString("Imagem"));
HttpURLConnection conexao = (HttpURLConnection) urlGetImg.openConnection();
InputStream inputImg = conexao.getInputStream();
Bitmap insertImg = BitmapFactory.decodeStream(inputImg);
nm.setImg(insertImg);
}
} catch (Exception e) {
e.printStackTrace();
}
}
class NewsModel{
private String nome;
private Bitmap img;
private String idAnime;
void setNome(String nome){
this.nome = nome;
}
void setImg(Bitmap img){
this.img = img;
}
void setIdAnime(String IdAnime){
this.idAnime = idAnime;
}
String getNome(){
return nome;
}
Bitmap getImg(){
return img;
}
String getId(){
return idAnime;
}
}
class VolleyAdapter extends BaseAdapter {
@Override
public int getCount() {
return arrNews.size();
}
@Override
public Object getItem(int i) {
return arrNews.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder vh;
if(view == null){
vh = new ViewHolder();
view = lf.inflate(R.layout.activity_item_grid_view, null);
vh.showImg = (ImageView) view.findViewById(R.id.imgItem);
vh.showNome = (TextView) view.findViewById(R.id.textItem);
}else {
vh = (ViewHolder) view.getTag();
}
NewsModel nm = arrNews.get(i);
vh.showNome.setText(nm.getNome());
vh.showImg.setImageBitmap(nm.getImg());
return view;
}
class ViewHolder{
TextView showNome;
ImageView showImg;
}
}
It has to return the values of the json that is in my webservice and put in a GridView JSON
{
"error": false,
"result": [
{
"Id": 33032,
"Nome": "Bakumatsu - Episódio 05",
"Imagem": "http://10.0.0.13/animes/img-anime/95110.jpg",
},
{
"Id": 33022,
"Nome": "Release the Spyce - Episódio 04",
"Imagem": "http://10.0.0.13/animes/img-anime/95617.jpg",
},
{
"Id": 33043,
"Nome": "Zombieland Saga - Episódio 05",
"Imagem": "http://10.0.0.13/animes/img-anime/94765.jpg",
},
{
"Id": 33029,
"Nome": "Merc Storia: Mukiryoku no Shounen to Bin no Naka no Shoujo
"Imagem": "http://10.0.0.13/animes/img-anime/95576.jpg",
},
{
"Id": 33015,
"Nome": "Tokyo Ghoul:re 2nd Season - Episódio 04",
"Imagem": "http://10.0.0.13/animes/img-anime/93889.jpg",
},
{
"Id": 32879,
"Nome": "Captain Tsubasa (2018) – Episódio 31",
"Imagem": "http://10.0.0.13/animes/img-anime/91260.jpg",
}