Skip to content

Instantly share code, notes, and snippets.

@samip5
Created November 20, 2019 13:54
Show Gist options
  • Save samip5/3a42aa3cdf7d82a543b618d2dda88b4a to your computer and use it in GitHub Desktop.
Save samip5/3a42aa3cdf7d82a543b618d2dda88b4a to your computer and use it in GitHub Desktop.
For StackOverFlow
[
{
"id": "ce13eccc-0054-4189-8b95-047f6e094303",
"short": "dflm",
"name": "TEST",
"zone": 122,
"type": "como",
"registration_plate": "",
"added": "2019-05-29T11:23:40.062842Z",
"serial": null,
"model_specification": null,
"status": "ready",
"bounty": 0,
"location": [
60.45579147338867,
22.301254272460938
],
"battery": 57,
"locked": true,
"updated": "2019-11-20T03:46:19Z",
"mileage": 0
}
]
E/AndroidRuntime: FATAL EXCEPTION: main
Process: fi.samipsolutions.voitracker, PID: 9573
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:502)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: org.json.JSONException: Value {"id":"ce13eccc-0054-4189-8b95-047f6e094303","short":"dflm","name":"TEST","zone":122,"type":"como","registration_plate":"","added":"2019-05-29T11:23:40.062842Z","serial":null,"model_specification":null,"status":"ready","bounty":0,"location":[60.45586395263672,22.301359176635742],"battery":52,"locked":true,"updated":"2019-11-20T04:33:23Z","mileage":0} at 0 of type org.json.JSONObject cannot be converted to JSONArray
at org.json.JSON.typeMismatch(JSON.java:101)
at org.json.JSONArray.getJSONArray(JSONArray.java:502)
at fi.samipsolutions.voitracker.activities.MainActivity$loadVehicles$jsonArrayRequest$1.onResponse(MainActivity.kt:43)
at fi.samipsolutions.voitracker.activities.MainActivity$loadVehicles$jsonArrayRequest$1.onResponse(MainActivity.kt:17)
at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:90)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:102)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
private fun loadVehicles() {
// Instantiate the RequestQueue
val queue = Volley.newRequestQueue(this)
val builder = StringBuilder()
val url = builder
.append("https://")
.append("<api-url-censored>")
.append("vehicle/status/ready?")
.append("lat=<censored>&lng=<censored>).toString()
Log.i("JSON_URL", url)
// Create request and listeners
val jsonArrayRequest = JsonArrayRequest(
Request.Method.GET, url, null,
Response.Listener { response ->
val json_data = response.getJSONArray(0)
for(i in 0 until json_data.length()) {
val item = json_data.getJSONObject(i)
if (item["type"] != "como") {
Log.d("JSON_SUCCESS", item["type"].toString())
}
}
},
Response.ErrorListener { error ->
Log.d("JSON_ERROR",error.toString())
}
)
// Add the request to the RequestQueue.
queue.add(jsonArrayRequest)
}
@samip5
Copy link
Author

samip5 commented Nov 20, 2019

Remove

val json_data = response.getJSONArray(0)

and replace

for(i in 0 until json_data.length()) {
val item = json_data.getJSONObject(i)

by

for(i in 0 until response.length()) {
val item = response.getJSONObject(i)

Oh, Wow. I feel stupid for not thinking of doing it like that. Thank you. It works. :)

@rajasekaranm
Copy link

Okay . FYI Stupids only can learn quick than others :-) Don't feel for mistakes 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment