gosulove gosulove - 1 month ago 4x
JSON Question

Android Json data(output from mysqli) is not working in TextView

I am doing some basic test for displaying json data that's output from PHP mysqli into TextView.

TextView mTxtDisplay;

String url = "";

protected void onCreate(Bundle savedInstanceState) {
mTxtDisplay = (TextView) findViewById(R.id.tv);

JsonObjectRequest jsObjRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

public void onResponse(JSONObject response) {
}, new Response.ErrorListener() {

public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub


RequestQueue requestQueue = Volley.newRequestQueue(this);

$sql="SELECT * FROM item where uid='1'";
$result = mysqli_query($conn, $sql);
$myArray = array();
if (mysqli_num_rows($result) > 0) {
while($row = $result->fetch_array(MYSQL_ASSOC)) {
$myArray[] = $row;
echo json_encode($myArray);


[{"iid":"482","uid":"1","item_id":"1","item_name":"Chicken Rice","item_price":"1","item_quantity":"1","status":"0"},
{"iid":"483","uid":"1","item_id":"1","item_name":"French Fries","item_price":"1","item_quantity":"1","status":"0"},

I think my json is correct? But it simply doesn't display in Textview, it's EMPTY! without any errors .

After that, I realized data in the below format is working well(normal hard code Array).


So that means the second json data without "[]" works fine but the first json data with "[]" is not working. But this shouldn't be the way right?

What's wrong with the coding?


It is because data within [] is JSONArray not JSONObject. Instead of JSONObjectRequest try StringRequest and then print your response into EditText/TextView. You can access elements of your array to by converting them into JSONArray.

JSONArray arr=new JSONArray(response);