I made a small MVC4 app wich returns data from an SQL server as an API, the local url is http://10.0.0.116:35577/api/articoli (I have already enabled network access to the local IIS instance, this actually stopped me for a whole day until I figured it out), the format it returns is the following:
[{"id":0,"desc":"Pizza"},{"id":1,"desc":"Pasta"},{"id":2,"desc":"Science"},{"id":3,"desc":"spaghetti"},{"id":4,"desc":"sfas"},{"id":5,"desc":"test"}]
calling http://10.0.0.116:35577/api/articoli/1 returns a single element:
{"id":1,"desc":"Pasta"}
I've been having trouble parsing this from my android app, from what I gather that array is supposed to have a header name or something, which is totally absent here and I can't really figure how to add it in here, or if I really need to have one at all.
From what I can gather the parser class I got from a tutorial is not suited to this kind of data and I can't really figure out how to make it work with such a result.
The parser is the following:
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
URL aaa = new URL(url);
URLConnection uConnection = aaa.openConnection();
uConnection.connect();
//is = httpEntity.getContent();
is = aaa.openStream();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, /*"UTF-8"*/ "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
I had to edit the url retrieval part of the parser because it tried to get the data by using POST, which the server application interpreted as an attempt at inserting data and called the wrong method,
And this is piece of code in which the parser is called, it's supposed to retrieve each element and then place each one as a row in a listview, which then clicked should call an activity that will read a single element:
private void fillDataJSON() {
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(URL);
JSONArray articoli;
try {
articoli = json.getJSONArray("Articoli");
if (articoli == null) {
throw new NullPointerException();
}
for(int i = 0; i< articoli.length(); i++) {
JSONObject c = articoli.getJSONObject(i);
String id = c.getString(KEY_ID);
String desc = c.getString(KEY_DESC);
HashMap<String, String> map = new HashMap<String, String>();
map.put(KEY_ID, id);
map.put(KEY_DESC, desc);
menuItems.add(map);
adapter = new SimpleAdapter(ApiTest.this, menuItems,
R.layout.activity_api_test_row,
new String[] { KEY_DESC },
new int[] { R.id.TextView01 });
}
} catch (JSONException e) {
e.printStackTrace();
}
}
The error I get is the following:
03-13 10:05:33.291: E/JSON Parser(11811): Error parsing data org.json.JSONException: Value [{"id":0,"desc":"Pizza"},{"id":1,"desc":"Pasta"},{"id":2,"desc":"Science"},{"id":3,"desc":"spaghetti"},{"id":4,"desc":"sfas"},{"id":5,"desc":"test"}] of type org.json.JSONArray cannot be converted to JSONObject
Can this be fixed on the android side or do I have to change how my server works?
The server just returns the data through the controller and everything else is left to the framework, example:
public IEnumerable<Articoli> GetAllArticoli()
{
return repository.GetAll();
}
doesn't do anything other than that