3
I have an API Restfull on the local Apache server that returns a list of database users (localhost/api/users).
I’m using the site Jsonlint to validate my JSON. When access via browser i take the result and validate it in Jsonlint, which returns me as valid JSON. An example returned by 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 am trying to access my application via Java following that explanation and I’m getting the following error:
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 am 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"));
}
}
}
@Renan edited.. however could you pass me your code? Thank you.
– funke
I had made a code to test the
JSONObject
, no more. When you edited the question it became clearer what the problem is.– Renan Gomes