0

Im working on my school project and im new in android programming but i have some programming experiance in c# and .net.

This is my code:

    public class ProizvodiAPI {

    public class ProizvodiVM implements Serializable
    {
        public Integer proizvodID;
        public String naziv;
        public String sifra;
        public BigDecimal cijena;
        public String jedinicaMjere;
        public String vrstaProizvoda;
    }
        public class ProizvodiLista implements Serializable
        {
            public  List<ProizvodiVM> proizvodi;
        }
public static void GetAllProizvode(final MyRunnable<ProizvodiLista> onSuccess)
    {

        RequestQueue queue = Volley.newRequestQueue(MyApp.getContext());
        String url = "Proizvodi/GetProizvodiVM";

        // Request a string response from the provided URL.
        StringRequest stringRequest = new StringRequest(Request.Method.GET, Config.urlApi + url,
                new Response.Listener<String>()
                {
                    @Override
                    public void onResponse(String response)
                    {
                        final Gson gson = MyGson.build();
                        final ProizvodiLista model = gson.fromJson(response, ProizvodiLista.class);
                        onSuccess.run(model);
                    }
                }, new Response.ErrorListener()
        {
            @Override
            public void onErrorResponse(VolleyError error)
            {

                Toast.makeText(MyApp.getContext() , "That didn't work", Toast.LENGTH_LONG).show();
            }
        });
        // Add the request to the RequestQueue.
        queue.add(stringRequest);
    }
}

Program crashes here:

final ProizvodiLista model = gson.fromJson(response, ProizvodiLista.class);

Is the problem with deserializing json, if it is, shoud i change the Java class and what to change ?

Here is my C# class in Web Api:

public class Proizvodi
    {
        public int ProizvodID { get; set; }
        public string Naziv { get; set; }
        public string Sifra { get; set; }
        public decimal Cijena { get; set; }

        public string JedinicaMjere { get; set; }
        public string VrstaProizvoda { get; set; }
    }

And this is response: json response

TariqN
  • 668
  • 2
  • 9
  • 27

1 Answers1

1

Without knowing the crash cause, my best guess is that you are creating a class just to wrap a list and that could be causing the crash. With the wrapper class your json should have a key that matches the list object name and the value would be a json array.

In order to deserialize a json array, check this answer.

Also try using @SerializedName annotation for your fields in ProizvodiVM. This way you can map the json key fields correctly.

Community
  • 1
  • 1
Natan
  • 1,867
  • 13
  • 24