Can not display data into the listview


#1

Hello guys , i am working on android and php web service , i am reaching now on the stage on fetching data from server and want to display them into listview, but i am not making , i see the json results into log file but i can not display them into my listview, i am told to jsonObject can not be converted to jsonArray which is the message i am getting , here is my source code,

help me on this , thanks ,and Jah Bless!!!


#2

What does the JSON you’re trying to parse look like?


#3

{
“result”{

“image”: 123,
“names”:“client_name”,
“joined”:“26/06/2018”

}
}

That is the json i am trying to retreive


#4

Ok, I can see the problem now:

You’re trying to retrieve contents of node result using code

var array = obj.getJSONArray("result")

There are two problems. The first is that JSON returned from the server is missing a colon after "result". Proper JSON should look like this:

{
	"result": {
		"image": 123,
		"names": "client_name",
		"joined": "26/06/2018"
	}
}

The second problem is that result is an object not an array, so you should rerieve it using:

var object = obj.getJSOnObject("result")

then

var image = object.get("image")
var names = object.get("client_name")
var joined = object.get("joined")

Your code would work if the server returned the following:

{
	“result”: [{
		“image”: 123,
		“names”: “client_name”,
		“joined”: “26/06/2018”
	}]
}

in which case "result" would be a one element array of objects.


#5

Thanks @forinil2 . I tried yur way which is collect bt i can not load data too. See what i have collected


#6

It seems that I was not clear enough in my explanation. You server currently returns an object containing field result, which is another object. You get the data like this:

val obj = JSONObject(o)
val obj2 = obj.getJSONObject("result")
val image = obj2.get("image")
val names = obj2.get("client_name")
val joined = obj2.get("joined")
val client = ClientObject(
	image, names, joined
)
listOfClients!!.add(client)

I suspect what you want is for your server to return an object containing field result, which in turn contains an array of objects.

In other words, you want your server to return this (note the square bracket that denote an array):

{
	“result”: [{
		“image”: 123,
		“names”: “client_name”,
		“joined”: “26/06/2018”
	}]
}

instead of this:

{
	"result": {
		"image": 123,
		"names": "client_name",
		"joined": "26/06/2018"
	}
}

Once you change your server output, you can retrieve data like this:

val obj = JSONObject(o)
val array = obj.getJSONArray("result")

array.forEach {
	val obj2 = it as JSONObject
	val image = obj2.get("image")
	val names = obj2.get("client_name")
	val joined = obj2.get("joined")
	val client = ClientObject(
		image, names, joined
	)
	listOfClients!!.add(client)
}

#7

Actually my server looks like this

{
“result”: {
“image”: 123,
“names”: “client_name”,
“joined”: “26/06/2018”
}
}

So i want this to be displayed into my listview

I am trying this
val obj = JSONObject(o)
val obj2 = obj.getJSONObject(“result”)
val image = obj2.get(“image”)
val names = obj2.get(“client_name”)
val joined = obj2.get(“joined”)
val client = ClientObject(
image, names, joined
)
listOfClients!!.add(client)

But my application is stopped working

That json returns many data . Is that why my application is stopped working


#8

Define “stopped working” - does it crash or does it not start at all?

Also what does that mean “json returns many data”?


#9

Now i get data @forinil using this
val obj = JSONObject(o)
val obj2 = obj.getJSONObject(“result”)
val image = obj2.get(“image”)
val names = obj2.get(“client_name”)
val joined = obj2.get(“joined”)
val client = ClientObject(
image, names, joined
)
listOfClients!!.add(client)

But i get one data can yu help me to loop thru so that i can get all data


#10

But you said yourself (and shown on screenshots): that your server returns this JSON:

{
	“result”: {
		“image”: 123,
		“names”: “client_name”,
		“joined”: “26/06/2018”
	}
}

which contains only one data set.

There is nothing to loop through.


#11

Aah my bad sorry for that bro i took screenshots for only one object but it returns almost nine whih are in database


#12

Can you post the code for the entire class (preferably as text with syntax highlighting and not as a screenshot)?


#13

private fun loadClients() {
val stringRequest = StringRequest(Request.Method.GET, URL,
Response.Listener { s ->
try {
val obj = JSONObject(s)

                            var data = obj.getJSONObject("result")
                            val image = data.getInt("image")
                            val names = data.getString("names")
                            val joined = data.getString("joined")

                            Log.d("InsideMe","I am in the inside of the block code")
                            Log.d("JsonImage",""+image)
                            Log.d("JsonName",names)
                            Log.d("JsonJoined",joined)

                            val client = ClientObject(R.drawable.persoicon,names, joined)

                            listOfClients!!.add(client)
                            myAdapter = ClientAdapter(this,listOfClients)
                            listViewClient.adapter = myAdapter


                } catch (e: JSONException) {
                    e.printStackTrace()
                }
            }, Response.ErrorListener { volleyError -> Toast.makeText(applicationContext, volleyError.message, Toast.LENGTH_LONG).show() })

    val requestQueue = Volley.newRequestQueue(this)
    requestQueue.add<String>(stringRequest)
}

#14

Considering the code you just provided and your previous screenshots, when you make a GET request to your server it returns only one client:

{
	"result": {
		"image": 123,
		"names": "client_name",
		"joined": "26/06/2018"
	}
}

which then is parsed and shown in list view.

If you want to see all the clients, first your server must return them like so (example for 4 clients):

{
	"result": [{
		"image": 123,
		"names": "client_name",
		"joined": "26/06/2018"
	},
	{
		"image": 123,
		"names": "client_name2",
		"joined": "26/06/2018"
	},
	{
		"image": 123,
		"names": "client_name3",
		"joined": "26/06/2018"
	},
	{
		"image": 123,
		"names": "client_name4",
		"joined": "26/06/2018"
	}]
}

Then you parse and show data like this:

val obj = JSONObject(o)
val array = obj.getJSONArray("result")

array.forEach {
	val obj2 = it as JSONObject
	val image = obj2.get("image")
	val names = obj2.get("client_name")
	val joined = obj2.get("joined")
	val client = ClientObject(
		image, names, joined
	)
	listOfClients!!.add(client)
}

myAdapter = ClientAdapter(this,listOfClients)
listViewClient.adapter = myAdapter

#15

I can not access .forEach function it is showing red color


#16

Try

val obj = JSONObject(o)
val array = obj.getJSONArray("result")

for (i in 0..array.length()-1) {
	val obj2 = array.get(i) as JSONObject
	val image = obj2.get("image")
	val names = obj2.get("client_name")
	val joined = obj2.get("joined")
	val client = ClientObject(
		image, names, joined
	)
	listOfClients!!.add(client)
}

myAdapter = ClientAdapter(this,listOfClients)
listViewClient.adapter = myAdapter

#17

I am still getting one data set which is first object into my listview but when i log into console i see all objects and data


#18

Can you post the code for both the server and Android client on GitHub (or BitBucket or SourceForge) and provide link the repos? Otherwise, I’m not sure how to help you further at this point.


#19
<?php include("connection.php"); $retreive_clients = "SELECT * FROM clients ORDER BY id DESC"; $result = mysqli_query($conn, $retreive_clients); while($row = mysqli_fetch_array($result)){ $json = json_encode( array( "result" => array([ "image" => 123, "names" => $row['names'], "joined" => $row['joined'] ]) ) ); echo ($json); } ?>

#20

We’re getting a bit off course since this is Kotlin and not PHP forum, but this is what your server should look like to produce properly formatted JSON:

<?php
include("connection.php");
$retrieve_clients = "SELECT * FROM clients ORDER BY id DESC";
$result = mysqli_query($conn, $retrieve_clients);
$output = array();
$count = 0;
while ($row = mysqli_fetch_array($result)) {
    $output[$count] = array("image" => 123, "names" => $row['names'], "joined" => $row['joined']);
    $count++;
}
$json = json_encode(array("result" => $output));
echo($json);
mysqli_close($conn);

If you have any more questions regarding the server, please ask them on PHP-related forum (or Stackoverflow maybe), before one of us gets banned :stuck_out_tongue: