I have a Restfull API on the local Apache server that returns a list of users from the database (localhost / api / users).
I'm using JSONLint to validate my JSON. When I access via browser I get the result and it is valid in JSONLint, which returns me as valid JSON. An example returned by the API:
{
"usuarios": [
{
"id": 167,
"contrato": 1,
"cod": "1212",
"nome": "Marcos Roberto Pavesi",
"email": "[email protected]",
"senha": "114fdfefd3d69799f0b6f73ef764d405",
"ativo": "S",
"setor": "1",
"max_add": "",
"dealer": 0
},
{
"id": 520,
"contrato": 1,
"cod": "",
"nome": "avaliador",
"email": "[email protected]",
"senha": "e10adc3949ba59abbe56e057f20f883e",
"ativo": "S",
"setor": "2",
"max_add": "",
"dealer": 0
}
]
}
However, I'm trying to access my application via Java by following explanation and I'm getting the following error: / p>
Exception in thread "main" org.json.JSONException:
A JSONObject text must begin with '{' at 1 [character 2 line 1]
at org.json.JSONTokener.syntaxError (JSONTokener.java:433)
at org.json.JSONObject. (JSONObject.java:197)
at org.json.JSONObject. (JSONObject.java:324)
at com.main.main.main (Main.java:14)
I'm accessing the api with the following code ...
public static void main(String[] args) {
JSONObject obj = new JSONObject("http://localhost:8080/api_carmaix/api/usuarios");
JSONArray arr = null;
try {
arr = obj.getJSONArray("usuarios");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int i = 0; i < arr.length(); i++)
{
System.out.println(arr.getJSONObject(i).getString("id"));
}
}
}