0

I'm developing an ASP.NET Web Api project with Entity Framework and other project with Android and the Volley lib.

The idea is the project in ASP.NET to be the server and the Android app the client.

Both projects already work. The ASP.NET project is already connected to SQL Server and returns values in json format from one database, and the client also parses json from an online server that I used for testing when I was following one tutorial.

ASP.NET Web Api Controller:

public class StoreController : ApiController
{
    // GET: api/Store
    public IEnumerable<bo> Get()
    {
        using (EGLA_PHCEntities services = new EGLA_PHCEntities())
        {
            return services.bo.Where(e => e.nmdos == "Ficha Servico 30").Where(e => e.fechada == false).ToList();
        }
    }
    ...
}

Android:

JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
        new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    JSONArray jsonArray = response.getJSONArray(null);

                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject employee = jsonArray.getJSONObject(i);
                        String firstName = employee.getString("fieldA");
                        String mail = employee.getString("fieldB");

                        mTextViewResult.append(firstName + ", " + mail + "\n\n");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        error.printStackTrace();
    }
});

The problem is that my project in ASP.NET does not return a name for the array of objects, and Android is waiting for a name.

The solution can be applied in one side or another. It can go through the ASP.NET project to return a name, or the project in Android to parse the json with an empty array name.

Modified because the list of fields is very extense:

[
    {
        "fieldA":"Something",
        "fieldB":"Store 30",
    }, 
    {
        "fieldA":"Something 2",
        "fieldB":"Store 30 2",
    }
]

The error that is returned in the Android app is "org.json.JSONException: No value for null". If I change

JSONArray jsonArray = response.getJSONArray(null);

to:

JSONArray jsonArray = response.getJSONArray("services");

The error returned is: "org.json.JSONException: No value for services"

Nelson Lopes
  • 117
  • 2
  • 13
  • can you clarify what you are asking? what exactly is the problem? you're returning a list of items. surely you can parse that on android side. what exactly is an "array name"? – Andrei Dragotoniu Jan 25 '19 at 09:16
  • You're right, I'm sorry. I did not include the error I'm getting on Android. Please check the update I made to the post (final part). – Nelson Lopes Jan 25 '19 at 09:38
  • Possible duplicate of [Get JSONArray without array name?](https://stackoverflow.com/questions/10164741/get-jsonarray-without-array-name) – Andrei Dragotoniu Jan 25 '19 at 10:27

0 Answers0