[英]Not able to convert JSON to Java class (call WebApi from Android app)
我正在我的學校項目上工作,我在android編程中還是新手,但是我在c#和.net中有一些編程經驗。
這是我的代碼:
public class ProizvodiAPI {
public class ProizvodiVM implements Serializable
{
public Integer proizvodID;
public String naziv;
public String sifra;
public BigDecimal cijena;
public Byte[] slikaThumb;
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);
}
}
程序在這里崩潰:
final ProizvodiLista model = gson.fromJson(response, ProizvodiLista.class);
反序列化json是否有問題,如果是,我應該更改Java類以及要更改的內容嗎?
這是Web Api中的C#類:
public class Proizvodi
{
public int ProizvodID { get; set; }
public string Naziv { get; set; }
public string Sifra { get; set; }
public decimal Cijena { get; set; }
public byte[] SlikaThumb { get; set; }
public string JedinicaMjere { get; set; }
public string VrstaProizvoda { get; set; }
}
根據您收到的響應,它是一個JSONArray而不是簡單的JSONObject。 正確反序列化。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.